How can I insert a Buffer value to mysql database? - mysql

So I have the following code:
let sql = `INSERT INTO users(email, name, surname, stellarAccount, stellarSeed) VALUES(?, ?, ?, ?, ?)`;
let new_user = [
mysql.escape(args.email),
mysql.escape(args.name),
mysql.escape(args.surname),
mysql.escape(keypair.publicKey()),
mysql.escape(utils.encrypt(Buffer.from(keypair.secret(), "utf-8"))),
];
console.log(await utils.encrypt(Buffer.from(keypair.secret(), "utf-8")));
con.query(sql, new_user, (err, results, fields) => {
if (err) {
return console.error(err.message);
}
console.log(results);
});
The problem is that instead of the encrypted value in mysql I have only blank space. I tried to console log the following:
utils.encrypt(Buffer.from(keypair.secret(), "utf-8"))
and it looked something like this:
<Buffer 01 02 02 00 78 45 8c 88 86 55 00 4f 23 8e 1f 80 a8 1d 3d c4 b0 6a 4c de 3e 60 db 43 51 8d 12 26 56 f3 70 1a 7b 01 89 65 c5 ea 7b 91 ff 71 f6 46 a6 e7 ... 162 more bytes>
In mysql the table charset is utf8mb4 and utf8mb4_0900_ai and the column that is blank is BLOB, also I wanted to set Not Null the column at it's giving me an error, I can't set BLOB to be not null ?
How can I insert the encrypted value in mysql db correcly ?
It is not for a password, I need this value afterwards, but I was thinking that encrypting it in the db would be much better than inserting it there as a plain text.

You can convert your buffer to utf-8 string and then you can save it.
buf1.toString('utf8');
See working

Related

How to convert Buffer to base64 image in Node js

I am getting data from my SQL database like this:
const mysql = require("mysql");
const connection = mysql.createConnection({
host: "localhost",
user: "root",
database: "database",
password : ''
});
//connection.release();
connection.connect(function(err) {
if (err) console.log(err);
});
connection.query("SELECT image FROM table WHERE id=(SELECT max(id) FROM table);", function (err, result, fields) {
if (err) console.log(err);
console.log(result);
});
/*Output:
[
RowDataPacket {
image: <Buffer 64 61 74 61 3a 69 6d 61 67 65 2f 6f 63 74 65 74 2d 73 74 72 65 61 6d 3b 62 61 73 65 36 34 2c 69 56 42 4f 52 77 30 4b 47 67 6f 41 41 41 41 4e 53 55 68 ... 27941 more bytes>
}
]
*/
How can I convert result to a base64 image in Node.js? For Example:
'data:image/png;base64,iVBORw0KGgoAAAANS'
Since you’re receiving a Buffer back as output, Buffer.toString('base64') will convert the raw binary data in the buffer to a base64 representation of the data.
Buffer.toString() can also take other encodings, you can read more about the other supported encodings in the docs.
So for your above code you would use this to get your image as base64
const dataImagePrefix = `data:image/png;base64,`
const query = 'SELECT image FROM table WHERE id=(SELECT max(id) FROM table)'
connection.query(query, (err, result, fields) => {
if (err) {
console.log(err)
// Exit after handling error
return
}
// Return all results and for each result row
// convert to a base64 string with the dataImagePrefix prepended to each
return results.map(result => `${dataImagePrefix}${result.image.toString('base64')}`)
})
You can do it very simple,
const generateImageFromBuffer = buffer => {
let _buffer = new Buffer.from(buffer, 'base64');
return _buffer.toString('base64');
};

Node MySQL connection resultset having issue

Connected Node using MySQL (using mysql2/promise)
on
const sql = `
SELECT *
FROM Applicant
WHERE ApplicationId = ?
`;
const result = await this.mysqlManager.Query(sql,number);
console.log(result);
public async Query(sql: any, parameters?: any, connection?: PoolConnection): Promise<any>
{
const con = connection || (await this.GetConnection());
try
{
const data = await con.query(sql, parameters);
return data;
}
catch (error)
{
con.release();
throw error;
}
}
but got response
ColumnDefinition {
_buf: <Buffer 3a 00 00 16 03 64 65 66 08 70 72 6f 70 65 6c 6c 64 09 41 70 70 6c 69 63 61 6e 74 09 41 70 70 6c 69 63 61 6e 74 05 50 43
69 74 79 05 50 43 69 74 79 0c ... 2376 more bytes>,
_clientEncoding: 'utf8',
_catalogLength: 3,
_catalogStart: 2329,
_schemaLength: 8,
_schemaStart: 2333,
_tableLength: 9,
_tableStart: 2342,
_orgTableLength: 9,
_orgTableStart: 2352,
_orgNameLength: 16,
_orgNameStart: 2379,
characterSet: 224,
encoding: 'utf8',
name: 'SalaryDayRangeTo',
columnLength: 80,
columnType: 253,
flags: 0,
decimals: 0
}
Can someone please help me with this issue?
const data = await con.query(sql, parameters); return data[0];
Since first element consist of data with other attributes consisting schema information.

ASC 122U NFC Reader compatible with EMV Contactless Cards

I am trying to read a EMV card using an APDU Command, however it seems the ACR122U external reader is blocking the APDU Command.
Select Command:
APDU-C -> 00 A4 04 00 0E 32 50 41 59 2E 53 59 53 2E 44 44 46 30 31 0E
APDU-R <- Error no response
Is it possible that the ACR122U reader is blocking the command ?
You want to SELECT FILE 1PAY.SYS.DDF01,
"Payment System Environment (PSE)"
To get the PSE directory and the card should response with Application Identifier (AID). but you set the LE=0E replace it to "00" .
Corrected APDU =>
PPSE = '00 a4 04 00 0e 32 50 41 59 2e 53 59 53 2e 44 44 46 30 31 00'
if The selection failed then the ADF doesn't exist (SW1/SW2=6A82)
if the selection is true then Application Identifier (AID) start command
Possible AID's:
A0000000031010
A0000000032020
A0000000041010
A0000000043060
AIDPrefix ='00 a4 04 00 07'

How to display an image from mysql using Express

I have a sql table 'animals' where there are blobs images. I found out how to upload images but not how to display them.
I would like to display the image which is called 'Dog' in my table.
Here is my code, where I print the result of my blob img.
let sql = 'SELECT * FROM animals WHERE file_name=\'Dog\''
connection.query(sql, (err,result) => {
console.log(result[0].img)
})
Here is the result of my code:
<Buffer ff d8 ff e0 00 10 4a 46 49 46 00 01 01 00 00 01 00 01 00 00 ff db 00 84 00 09 06 07 13 12 12 15 13 13 13 16 16 15 15 18 18 17 16 18 15 17 15 17 17 16 ... >
Is there is any way to display that picture?
Thank you.
You can use the Fetch API to get the resource on your web page.
You can display the image like this :
fetch('http://localhost:1234/your_api')
.then(function(response) {
return response.blob();
})
.then(function(myBlob) {
var objectURL = URL.createObjectURL(myBlob);
document.querySelector("#databaseimage").src = objectURL;
});
In HTML :
<img id="databaseimage"/>
You can read more about Fetch API here :
https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch

Need help reverse engineering a CRC16

I'm trying to connect to the Safecom TA-810 (badge/registration system) to automate the process of calculating how long employee's have worked each day. Currently this is done by:
Pulling the data into the official application
Printing a list of all 'registrations'
Manually entering the values from the printed lists into our HR application
This is a job that can take multiple hours which we'd like to see automated. So far the official tech support has been disappointing and refused to share any details.
Using wireshark I have been capturing the UDP transmissions and have pretty much succeeded in understanding how the protocol is built up. I'm only having issues with what i suppose is a CRC field. I don't know how it is calculated (CRC type and parameters) and using which fields ...
This is how a message header looks like:
D0 07 71 BC BE 3B 00 00
D0 07 - Message type
71 BC - This i believe is the CRC
BE 3B - Some kind of session identifier. Stays the same for every message after the initial message (initial message has '00 00' as value)
00 00 - Message number. '01 00', '02 00', '03 00'
Some examples:
Header only examples
E8 03 17 FC 00 00 00 00 -> initial request (#0, no session nr)
D0 07 71 BC BE 3B 00 00 -> Initial response (#0, device sends a session nr)
4C 04 EF BF BE 3B 06 00 -> Message #6, still using the same session # as the initial response
Larger example, which has data
0B 00 07 E1 BE 3B 01 00 7E 45 78 74 65 6E 64 46 6D 74
I've also been trying to figure this out by reading the disassembled code from the original application. The screenshot below happens before the socket.sendto and seems to be related.
Any help will be extremely appreciated.
EDIT: Made some success with debugging the application using ollydbg. The CRC appears in register (reversed) EDX at the selected line in the following screenshot.
Take a look at CRC RevEng. If you can correctly identify the data that the CRC is operating on and the location of the CRC, you should be able to determine the CRC parameters. If it is a CRC.
I've managed to create a php script that does the CRC calculation by debugging the application using OllyDbg.
The CRC is calculated by Adding up every 2 bytes (every short). if the result is larger than a short, the 'most significant short' is added to the 'least significant short' until the result fits in a short. Finally, the CRC (short) is inverted.
I'll add my php script for completeness:
<?php
function CompareHash($telegram)
{
$telegram = str_replace(" ", "", $telegram);
$telegram_crc = substr($telegram, 4, 4);
$telegram = str_replace($telegram_crc, "0000", $telegram);
echo "Telegram: ", $telegram, ', Crc: ', $telegram_crc, ' (', hexdec($telegram_crc), ')<br />';
$crc = 0;
$i = 0;
while ($i < strlen($telegram))
{
$short = substr($telegram, $i, 4);
if (strlen($short) < 4) $short = $short . '00';
$crc += hexdec($short);
$i += 4;
}
echo "Crc: ", $crc, ', inverse: ', ~$crc;
// Region "truncate CRC to Int16"
while($crc > hexdec('FFFF'))
{
$short = $crc & hexdec ('FFFF');
// Region "unsigned shift right by 16 bits"
$crc = $crc >> 16;
$crc = $crc & hexdec ('FFFF');
// End region
$crc = $short + $crc;
}
// End region
// Region "invert Int16"
$crc = ~$crc;
$crc = $crc & hexdec ('FFFF');
// End region
echo ', shifted ', $crc;
if (hexdec($telegram_crc) == $crc)
{
echo "<br />MATCH!!! <br />";
}
else
{
echo "<br />failed .... <br />";
}
}
$s1_full = "E8 03 17 FC 00 00 00 00";
$s2_full = "D0 07 71 BC BE 3B 00 00";
$s3_full = "D0 07 4E D4 E1 23 00 00";
$s4_full = "D0 07 35 32 BE 3B 07 00 7E 44 65 76 69 63 65 4E 61 6D 65 3D 54 41 38 31 30 00";
$s5_full = "0B 00 39 6C BE 3B 05 00 7E 52 46 43 61 72 64 4F 6E";
CompareHash($s1_full);
CompareHash($s2_full);
CompareHash($s3_full);
CompareHash($s4_full);
CompareHash($s5_full);
?>
Thanks for the feedback!