Repository navigation
Expand file tree
/
Copy pathExampleHash.js
More file actions
55 lines (48 loc) · 1.44 KB
/
Copy pathExampleHash.js
File metadata and controls
55 lines (48 loc) · 1.44 KB
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
/**
* An example for hashing of Strings featuring:
* - An out of the box working Example
* - sha-512 digest
* - Utf8 Encoding of Strings
* - base64 Encoding of byte arrays
* - Logging of exceptions
*/
var crypto = require("crypto"),
winston = require("winston");
const logger = winston.createLogger({
format: winston.format.combine(
winston.format.splat(),
winston.format.simple()
),
transports: [
new winston.transports.Console({
format: winston.format.simple(),
handleExceptions: true
})
]
});
const demonstrateHash = () => {
try {
// replace with your actual Strings
var exampleString =
"Text that should be authenticated by comparing the hash of it!";
var exampleString2 =
"Text that should be authenticated by comparing the hash of it! - 2";
exampleString = exampleString.toString("utf8");
exampleString2 = exampleString2.toString("utf8");
//create a hash object
var hashObject = crypto.createHash("sha512");
hashObject.setEncoding("base64");
//update the hash object with data as often as required
hashObject.write(exampleString);
hashObject.write(exampleString2);
hashObject.end();
// create the hash values
var digest = hashObject.read();
logger.info("Digest of the Strings: %s", digest);
} catch (error) {
logger.error(error.message);
}
};
demonstrateHash();
// for unit testing purposes
module.exports = { demonstrateHash, logger };