| import native from './native.js'; |
| import { defaultHashLength } from '../base/hash-fn.js'; |
| |
| |
| |
| export const normalizeInput = (input, encoding) => { |
| if (input instanceof Buffer) { |
| return input; |
| } |
| if (typeof input === 'string') { |
| return Buffer.from(input, encoding); |
| } |
| return Buffer.from(input); |
| }; |
| |
| |
| |
| export function hash(input, { length = defaultHashLength } = {}) { |
| return native.hash(normalizeInput(input), length); |
| } |
| |
| |
| |
| |
| |
| export function deriveKey(context, material, { length = defaultHashLength } = {}) { |
| const hasher = new native.Hasher(undefined, context); |
| hasher.update(normalizeInput(material)); |
| const result = Buffer.alloc(length); |
| hasher.digest(result); |
| return result; |
| } |
| |
| |
| |
| export function keyedHash(key, input, { length = defaultHashLength } = {}) { |
| const hasher = new native.Hasher(key); |
| hasher.update(normalizeInput(input)); |
| const result = Buffer.alloc(length); |
| hasher.digest(result); |
| return result; |
| } |
| |