All files / src/apis/shared/platform/crypto node.ts

100% Statements 7/7
100% Branches 0/0
100% Functions 3/3
100% Lines 7/7

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 31 32 33 34 35 36 37 38 39 40 41 42 43 44 456x                     6x 6x                   6x 6x                       6x         6x        
import crypto from "crypto";
import type { BinaryAlike } from "./shared";
 
/**
 *
 *
 * @export
 * @param {Uint8Array} array1
 * @param {Uint8Array} array2
 * @return {boolean} boolean
 */
export function timingSafeEqual(array1: Uint8Array, array2: Uint8Array): boolean {
  return crypto.timingSafeEqual(Buffer.from(array1), Buffer.from(array2));
}
 
/**
 *
 *
 * @export
 * @param {number} size
 * @return {Uint8Array} Uint8Array
 */
export function randomBytes(size: number): Uint8Array {
  return new Uint8Array(crypto.randomBytes(size));
}
 
/**
 *
 *
 * @export
 * @param {string} algorithm
 * @param {BinaryAlike} key
 * @param {BinaryAlike} data
 * @return {Uint8Array} Uint8Array
 */
export function hmac(
  algorithm: string,
  key: BinaryAlike,
  data: BinaryAlike
): Uint8Array {
  return new Uint8Array(
    crypto.createHmac(algorithm, Buffer.from(key)).update(Buffer.from(data)).digest()
  );
}