ethers.js/src.ts/utils/sha2.ts

25 lines
547 B
TypeScript
Raw Normal View History

2018-06-13 22:39:39 +03:00
'use strict';
import * as _hash from 'hash.js';
2018-06-17 23:47:28 +03:00
import { arrayify, Arrayish } from './bytes';
2018-06-13 22:39:39 +03:00
interface HashFunc {
(): HashFunc;
update(chunk: Uint8Array): HashFunc;
digest(encoding?: string): string;
}
const _sha256: HashFunc = _hash['sha256'];
const _sha512: HashFunc = _hash['sha512'];
2018-06-15 11:18:17 +03:00
export function sha256(data: Arrayish): string {
2018-06-13 22:39:39 +03:00
return '0x' + (_sha256().update(arrayify(data)).digest('hex'));
}
2018-06-15 11:18:17 +03:00
export function sha512(data: Arrayish): string {
2018-06-13 22:39:39 +03:00
return '0x' + (_sha512().update(arrayify(data)).digest('hex'));
}