1 overview

JsSHA is an encryption library that uses JS+TS to implement a complete set of SHA encryption algorithms, including:

  • SHA1
  • SHA-224/256/384/512
  • SHA3-224/256/384/512
  • SHAKE128/256
  • cSHAKE128/256
  • KMAC128/256

Here are some examples of using encryption algorithms.

2 Install and import

Install using NPM or CNPM:

npm install --save jssha
# or
cnpm install --save jssha
Copy the code

When importing, import according to type in package.json:

const jsSHA = require("jssha") //commonjs
import jsSHA from "jssha"      //module
Copy the code

if

"type":"commonjs"
Copy the code

Use the

const jsSHA = require("jssha")
Copy the code

3 the hash

A simple example is as follows:

const jsSHA = require("jssha")
const shaObj = new jsSHA("SHA-512"."TEXT", {encoding:"UTF8"})

shaObj.update("test")
console.log(shaObj.getHash("HEX"))
Copy the code

A shaObj is declared, the plaintext is passed by update, and the hash is obtained by getHash(“HEX”). Update can be called multiple times, and getHash is used.

JsSHA () takes three parameters: hash algorithm, input format, and (optional) parameter Settings.

3.1 Hash algorithm

The first parameter is the hash algorithm, which supports the following algorithms:

  • SHA-1
  • SHA-224
  • SHA-256
  • SHA-384
  • SHA-512
  • SHA3-224
  • SHA3-256
  • SHA3-384
  • SHA3-512
  • SHAKE128
  • SHAKE256

You are advised to use SHA-2 (SHA-224, 256/384/512) or SHA-3 (SHA3-224/256/384/512) instead of SHA-1.

SHAKE is short for Secure Hash Algorithm and KECCAK, which is defined in FISP 202. It is similar to SHA-3, but the output is unlimited. The number of bits to be output must be specified. Otherwise, an error will be generated:

Add the parameters to getHash:

const jsSHA = require("jssha")
const shaObj = new jsSHA("SHAKE128"."TEXT", {encoding:"UTF8"})

shaObj.update("test")
console.log(shaObj.getHash("HEX", {outputLen:1024}))
Copy the code

3.2 Input Format

The input format is as follows:

  • HEX
  • TEXT
  • B64
  • BYTES
  • ARRAYBUFFER
  • UNIT8ARRAY

3.3 Parameter Options

Two are commonly used:

  • encoding: specifies the code. Values are allowedUTF8/UTF16BE/UTF16LE
  • numRounds: Number of hashes

Other parameters can be viewed source code.

3.4 getHash

GetHash is a function that gets the result of the hash. The first argument can be:

  • HEX
  • B64
  • BYTES
  • UINT8ARRAY
  • ARRAYBUFFER

In the case of HEX, the output length option can be used. For HEX, the outputUpper option can be used to indicate whether the input is uppercase:

4 HMAC

HMAC is a method of message authentication based on hash functions and keys. When using HMAC, you need to specify the keys:

const jsSHA = require("jssha")
const shaObj = new jsSHA("SHA3-512"."TEXT", {hmacKey: {value:'secret key'.format:'TEXT'}
})

shaObj.update("test")
console.log(shaObj.getHash("HEX"))
Copy the code

5 cSHAKE

CSHAKE can be thought of as a “custom” version of SHAKE that requires additional customization parameters:

const jsSHA = require("jssha")
const shaObj = new jsSHA("CSHAKE128"."TEXT", {customization: {value:"test".format:"TEXT"}
})

shaObj.update("test")
console.log(shaObj.getHash("HEX", {outputLen:1024}))
Copy the code

6 KMAC

KMAC (KECCAK Message Authentication Code) is an algorithm based on KECCAK, which needs to provide kmacKey ‘parameter:

const jsSHA = require("jssha")
const shaObj = new jsSHA("KMAC128"."TEXT", {kmacKey: {value:"secret key".format:"TEXT"}
})

shaObj.update("test")
console.log(shaObj.getHash("HEX", {outputLen:1024}))
Copy the code