Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | 1x 1x 1x 5x 5x 12x 12x 12x 12x 6x 6x 1x 5x 193x 2x 3x | import { Sha512, type IHasher } from '../hasher';
import { xor } from '../utils/xor';
import type { IMessageAuthenticator } from './IMessageAuthenticator';
export class HMAC implements IMessageAuthenticator {
public constructor(
private readonly key: Uint8Array,
private readonly hasher: IHasher = new Sha512()
) {}
public sign(message: Uint8Array): Uint8Array {
const hashedKey = this.hasher.hash(this.key);
const opad = new Uint8Array(hashedKey.length).fill(0x5C);
const ipad = new Uint8Array(hashedKey.length).fill(0x36);
return this.hasher.hash(new Uint8Array([...xor(hashedKey, opad), ...this.hasher.hash(xor(hashedKey, ipad)), ...message]));
}
public verify(message: Uint8Array, tag: Uint8Array): boolean {
const expectedTag = this.sign(message);
if (tag.length !== expectedTag.length)
return false;
for (let i = 0; i < tag.length; i++) {
if (tag[i] !== expectedTag[i])
return false;
}
return true;
}
}
|