UNPKG

tweetnacl-ts

Version:

Port of TweetNaCl cryptographic library to TypeScript (and ES6)

32 lines 1.04 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); var array_1 = require("./array"); var hash_1 = require("./hash"); function auth(msg, key) { var out = array_1.ByteArray(32); out.set(hmac(msg, key).subarray(0, 32)); return out; } exports.auth = auth; var BLOCK_SIZE = 128; var HASH_SIZE = 64; function hmac(msg, key) { var buf = array_1.ByteArray(BLOCK_SIZE + Math.max(HASH_SIZE, msg.length)); var i, innerHash; if (key.length > BLOCK_SIZE) key = hash_1.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_1.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_1.hash(buf.subarray(0, BLOCK_SIZE + innerHash.length)); } exports.auth_full = hmac; //# sourceMappingURL=auth.js.map