UNPKG

4.1 kBPlain TextView Raw
1import { keccak224, keccak384, keccak256 as k256, keccak512 } from 'ethereum-cryptography/keccak'
2const createHash = require('create-hash')
3import * as rlp from 'rlp'
4import { toBuffer, setLengthLeft } from './bytes'
5import { assertIsString, assertIsBuffer, assertIsArray, assertIsHexString } from './helpers'
6
7/**
8 * Creates Keccak hash of a Buffer input
9 * @param a The input data (Buffer)
10 * @param bits (number = 256) The Keccak width
11 */
12export const keccak = function (a: Buffer, bits: number = 256): Buffer {
13 assertIsBuffer(a)
14 switch (bits) {
15 case 224: {
16 return keccak224(a)
17 }
18 case 256: {
19 return k256(a)
20 }
21 case 384: {
22 return keccak384(a)
23 }
24 case 512: {
25 return keccak512(a)
26 }
27 default: {
28 throw new Error(`Invald algorithm: keccak${bits}`)
29 }
30 }
31}
32
33/**
34 * Creates Keccak-256 hash of the input, alias for keccak(a, 256).
35 * @param a The input data (Buffer)
36 */
37export const keccak256 = function (a: Buffer): Buffer {
38 return keccak(a)
39}
40
41/**
42 * Creates Keccak hash of a utf-8 string input
43 * @param a The input data (String)
44 * @param bits (number = 256) The Keccak width
45 */
46export const keccakFromString = function (a: string, bits: number = 256) {
47 assertIsString(a)
48 const buf = Buffer.from(a, 'utf8')
49 return keccak(buf, bits)
50}
51
52/**
53 * Creates Keccak hash of an 0x-prefixed string input
54 * @param a The input data (String)
55 * @param bits (number = 256) The Keccak width
56 */
57export const keccakFromHexString = function (a: string, bits: number = 256) {
58 assertIsHexString(a)
59 return keccak(toBuffer(a), bits)
60}
61
62/**
63 * Creates Keccak hash of a number array input
64 * @param a The input data (number[])
65 * @param bits (number = 256) The Keccak width
66 */
67export const keccakFromArray = function (a: number[], bits: number = 256) {
68 assertIsArray(a)
69 return keccak(toBuffer(a), bits)
70}
71
72/**
73 * Creates SHA256 hash of an input.
74 * @param a The input data (Buffer|Array|String)
75 */
76const _sha256 = function (a: any): Buffer {
77 a = toBuffer(a)
78 return createHash('sha256').update(a).digest()
79}
80
81/**
82 * Creates SHA256 hash of a Buffer input.
83 * @param a The input data (Buffer)
84 */
85export const sha256 = function (a: Buffer): Buffer {
86 assertIsBuffer(a)
87 return _sha256(a)
88}
89
90/**
91 * Creates SHA256 hash of a string input.
92 * @param a The input data (string)
93 */
94export const sha256FromString = function (a: string): Buffer {
95 assertIsString(a)
96 return _sha256(a)
97}
98
99/**
100 * Creates SHA256 hash of a number[] input.
101 * @param a The input data (number[])
102 */
103export const sha256FromArray = function (a: number[]): Buffer {
104 assertIsArray(a)
105 return _sha256(a)
106}
107
108/**
109 * Creates RIPEMD160 hash of the input.
110 * @param a The input data (Buffer|Array|String|Number)
111 * @param padded Whether it should be padded to 256 bits or not
112 */
113const _ripemd160 = function (a: any, padded: boolean): Buffer {
114 a = toBuffer(a)
115 const hash = createHash('rmd160').update(a).digest()
116 if (padded === true) {
117 return setLengthLeft(hash, 32)
118 } else {
119 return hash
120 }
121}
122
123/**
124 * Creates RIPEMD160 hash of a Buffer input.
125 * @param a The input data (Buffer)
126 * @param padded Whether it should be padded to 256 bits or not
127 */
128export const ripemd160 = function (a: Buffer, padded: boolean): Buffer {
129 assertIsBuffer(a)
130 return _ripemd160(a, padded)
131}
132
133/**
134 * Creates RIPEMD160 hash of a string input.
135 * @param a The input data (String)
136 * @param padded Whether it should be padded to 256 bits or not
137 */
138export const ripemd160FromString = function (a: string, padded: boolean): Buffer {
139 assertIsString(a)
140 return _ripemd160(a, padded)
141}
142
143/**
144 * Creates RIPEMD160 hash of a number[] input.
145 * @param a The input data (number[])
146 * @param padded Whether it should be padded to 256 bits or not
147 */
148export const ripemd160FromArray = function (a: number[], padded: boolean): Buffer {
149 assertIsArray(a)
150 return _ripemd160(a, padded)
151}
152
153/**
154 * Creates SHA-3 hash of the RLP encoded version of the input.
155 * @param a The input data
156 */
157export const rlphash = function (a: rlp.Input): Buffer {
158 return keccak(rlp.encode(a))
159}