Would you like to clone this notebook?

When you clone a notebook you are able to make changes without affecting the original notebook.

Cancel

Hash Encodings Example

node v14.20.1
version: 1.0.0
endpointsharetweet
A simple educational example of how to generate a hash and encode it in different string encodings. A Github gist of this can be found at: https://gist.github.com/grempe/fc268565402b0a44638fb3439a1d981c
Generate a hash as a `Uint8Array` of bytes.
// include some Javascript libraries 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 generated_hash = hash_update.digest()
# Encode as Hex You can encode the bytes as `hex`. It encodes each byte to two characters so a hex string will be double the size of the number of bytes in the hash output. A hex string is made up of only the characters `a-f` and the numbers `0-9` and is not case sensitive.
const asHex = generated_hash.toString('hex')
# Encode as Base64 Or, you can encode the bytes as `base64` which is more compact than hex, but it is case sensitive and contains characters that are less human friendly to type, copy or speak.
const asBase64 = generated_hash.toString('base64')
Loading…

no comments

    sign in to comment