UNPKG

tweetnacl-ts

Version:

Port of TweetNaCl cryptographic library to TypeScript (and ES6)

29 lines 916 B
import { ByteArray } from './array'; import { hash } from './hash'; export function auth(msg, key) { var out = ByteArray(32); out.set(hmac(msg, key).subarray(0, 32)); return out; } var BLOCK_SIZE = 128; var HASH_SIZE = 64; function hmac(msg, key) { var buf = ByteArray(BLOCK_SIZE + Math.max(HASH_SIZE, msg.length)); var i, innerHash; if (key.length > BLOCK_SIZE) key = hash(key); for (i = 0; i < BLOCK_SIZE; i++) buf[i] = 0x36; for (i = 0; i < key.length; i++) buf[i] ^= key[i]; buf.set(msg, BLOCK_SIZE); innerHash = hash(buf.subarray(0, BLOCK_SIZE + msg.length)); for (i = 0; i < BLOCK_SIZE; i++) buf[i] = 0x5c; for (i = 0; i < key.length; i++) buf[i] ^= key[i]; buf.set(innerHash, BLOCK_SIZE); return hash(buf.subarray(0, BLOCK_SIZE + innerHash.length)); } export var auth_full = hmac; //# sourceMappingURL=auth.js.map