1 import { use as chaiUse, expect } from 'chai';
2 import chaiAsPromised from 'chai-as-promised';
4 import { importKey, signData, verifyData } from '../../lib/subtle/hmac';
5 import { stringToUtf8Array } from '../../lib/utils';
7 chaiUse(chaiAsPromised);
9 describe('Subtle - HMAC-SHA256 helpers', () => {
10 it('importKey - throws on short keys', async () => {
11 const secretBytesTooShort = crypto.getRandomValues(new Uint8Array(16)); // 32 bytes required with SHA256
12 await expect(importKey(secretBytesTooShort)).to.be.rejectedWith(/key is too short/);
15 it('importKey/signData/verifyData - correctly confirms authenticity of signed data', async () => {
16 const secretBytes = crypto.getRandomValues(new Uint8Array(32));
17 const key = await importKey(secretBytes);
18 const data = stringToUtf8Array('hello world');
19 const signed = await signData(key, data);
21 const verified = await verifyData(key, signed, data);
22 expect(verified).to.be.true;
24 // check that different `data` does not verify
25 await expect(verifyData(key, signed, new Uint8Array([1, 2, 3]))).to.eventually.be.false;