-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathGenerateSha256Base64HexCryptoJs.js
40 lines (30 loc) · 1.42 KB
/
GenerateSha256Base64HexCryptoJs.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
var CryptoJS = require("crypto-js");
console.log('Generate a SHA-256 hash, Base64 en- and decoding and hex conversions');
const plaintext = 'The quick brown fox jumps over the lazy dog';
console.log('plaintext: ', plaintext);
var sha256Value = calculateSha256(plaintext);
console.log('sha256Value (hex) length: ' + sha256Value.sigBytes + ' data: ' + bytesToHex(sha256Value));
var sha256Base64 = base64Encoding(sha256Value);
console.log('sha256Value (base64): ' + sha256Base64);
var sha256ValueDecoded = base64Decoding(sha256Base64);
console.log('sha256Base64 decoded to a byte array:');
console.log('sha256Value (hex) length: ' + sha256ValueDecoded.sigBytes + ' data: ' + bytesToHex(sha256ValueDecoded));
var sha256HexString = 'd7a8fbb307d7809469ca9abcb0082e4f8d5651e46d3cdb762d02d0bf37c9e592';
var sha256Hex = hexStringToByteArray(sha256HexString);
console.log('sha256HexString converted to a byte array:');
console.log('sha256Value (hex) length: ' + sha256Hex.sigBytes + ' data: ' + (sha256Hex));
function calculateSha256(input) {
return CryptoJS.SHA256(input);
}
function bytesToHex(input) {
return input.toString(CryptoJS.enc.Hex)
}
function hexStringToByteArray(input) {
return CryptoJS.enc.Hex.parse(input);
}
function base64Encoding(input) {
return CryptoJS.enc.Base64.stringify(input);
}
function base64Decoding(input) {
return CryptoJS.enc.Base64.parse(input);
}