Skip to content

Instantly share code, notes, and snippets.

@grempe
Created July 30, 2021 21:12
Show Gist options
  • Save grempe/fc268565402b0a44638fb3439a1d981c to your computer and use it in GitHub Desktop.
Save grempe/fc268565402b0a44638fb3439a1d981c to your computer and use it in GitHub Desktop.
Hash Encodings Example
// filename: hashes.js
// For Node.js
// Run this file in your terminal using node:
// $ node hashes.js
// NOTE : You can find an interactive Runkit version of this file here:
// https://runkit.com/truestamp/610455fa5f22db001adf76a8
const crypto = require('crypto')
const message = 'Truestamp rocks!'
// Create a hash of the `text` as a `Uint8Array` byte array
const hash = crypto.createHash('sha256')
const hash_update = hash.update(message, 'utf-8')
const digest = hash_update.digest()
// Output the hash using different encodings
console.log("as Bytes")
console.log(digest)
//= <Buffer 20 6c 28 6f ba 2e 26 86 93 22 aa cb cb bb 69 54 09 68 b0 17 a6 00 03 9c 6a d9 11 1b f9 9b a2 e1>
console.log("as Hex")
console.log(digest.toString('hex'))
//= 206c286fba2e26869322aacbcbbb69540968b017a600039c6ad9111bf99ba2e1
console.log("as Base64")
console.log(digest.toString('base64'))
//= IGwob7ouJoaTIqrLy7tpVAlosBemAAOcatkRG/mbouE=
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment