//--------------------------------------------------------------------------- // @syndicate-lang/core, an implementation of Syndicate dataspaces for JS. // Copyright (C) 2016-2021 Tony Garnock-Jones // // This program is free software: you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by // the Free Software Foundation, either version 3 of the License, or // (at your option) any later version. // // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. // // You should have received a copy of the GNU General Public License // along with this program. If not, see . //--------------------------------------------------------------------------- import { Bytes, underlying } from '@preserves/core'; import * as node_crypto from 'crypto'; export const KEY_LENGTH = 16; // 128 bits export const newKey: () => Promise = (typeof crypto !== 'undefined' && 'getRandomValues' in crypto) ? (async () => { const bs = new Bytes(KEY_LENGTH); crypto.getRandomValues(underlying(bs)); return bs; }) : (async () => Bytes.from(node_crypto.randomBytes(KEY_LENGTH))); export const mac: (secretKey: Bytes, data: Bytes) => Promise = (typeof crypto !== 'undefined' && 'subtle' in crypto) ? (async (secretKey, data) => { if (secretKey.length !== KEY_LENGTH) throw new Error("Invalid key length"); const k = await window.crypto.subtle.importKey( "raw", underlying(secretKey), { hash: 'SHA-256', name: 'HMAC', }, false, ['sign']); const bs = await window.crypto.subtle.sign({ name: 'HMAC' }, k, underlying(data)); return Bytes.from(new Uint8Array(bs, 0, KEY_LENGTH)); }) : (typeof node_crypto.createHmac !== 'undefined') ? (async (secretKey, data) => { const hmac = node_crypto.createHmac('sha256', underlying(secretKey)); hmac.update(underlying(data)); return Bytes.from(hmac.digest().subarray(0, KEY_LENGTH)); }) : (async (_secretKey, _data) => { throw new Error('No HMAC SHA-256 available'); });