Node/Crypto

From Fundamental Ramen
< Node
Revision as of 04:20, 25 August 2020 by Tacoball (talk | contribs) (Created page with "== HmacSHA256 with crypto & crypto-js == <source lang="js"> function myHashA(payload) { const crypto = require("crypto"); const secret = "0123456789abcdef"; const secret...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

HmacSHA256 with crypto & crypto-js

function myHashA(payload) {
  const crypto = require("crypto");
  const secret = "0123456789abcdef";
  const secretBin = Buffer.from(secret, "hex");
  return crypto.createHmac("sha256", secretBin)
    .update(JSON.stringify(payload))
    .digest()
    .toString("hex");
}

function myHashB(payload) {
  const CryptoJS = require("crypto-js");
  const secret = "0123456789abcdef";
  const secretBin = CryptoJS.enc.Hex.parse(secret);
  return CryptoJS.HmacSHA256(JSON.stringify(payload), secretBin)
    .toString(CryptoJS.enc.Hex);
}

console.log(myHashA("fuck"));
console.log(myHashB("fuck"));