@@ -3,52 +3,133 @@ const createHash = require('create-hash')
3
3
const ethjsUtil = require ( 'ethjs-util' )
4
4
import * as rlp from 'rlp'
5
5
import { toBuffer , setLengthLeft } from './bytes'
6
+ import { assertIsString , assertIsBuffer , assertIsArray , assertIsHexString } from './helpers'
6
7
7
8
/**
8
- * Creates Keccak hash of the input
9
- * @param a The input data (Buffer|Array|String|Number) If the string is a 0x-prefixed hex value
10
- * it's interpreted as hexadecimal, otherwise as utf8.
11
- * @param bits The Keccak width
9
+ * Creates Keccak hash of a Buffer input
10
+ * @param a The input data (Buffer)
11
+ * @param bits (number = 256) The Keccak width
12
12
*/
13
- export const keccak = function ( a : any , bits : number = 256 ) : Buffer {
14
- if ( typeof a === 'string' && ! ethjsUtil . isHexString ( a ) ) {
15
- a = Buffer . from ( a , 'utf8' )
16
- } else {
17
- a = toBuffer ( a )
18
- }
19
-
20
- if ( ! bits ) bits = 256
21
-
13
+ export const keccak = function ( a : Buffer , bits : number = 256 ) : Buffer {
14
+ assertIsBuffer ( a )
22
15
return createKeccakHash ( `keccak${ bits } ` )
23
16
. update ( a )
24
17
. digest ( )
25
18
}
26
19
27
20
/**
28
21
* Creates Keccak-256 hash of the input, alias for keccak(a, 256).
29
- * @param a The input data (Buffer|Array|String|Number )
22
+ * @param a The input data (Buffer)
30
23
*/
31
- export const keccak256 = function ( a : any ) : Buffer {
24
+ export const keccak256 = function ( a : Buffer ) : Buffer {
32
25
return keccak ( a )
33
26
}
34
27
35
28
/**
36
- * Creates SHA256 hash of the input.
37
- * @param a The input data (Buffer|Array|String|Number)
29
+ * Creates Keccak hash of a utf-8 string input
30
+ * @param a The input data (String)
31
+ * @param bits (number = 256) The Keccak width
38
32
*/
39
- export const sha256 = function ( a : any ) : Buffer {
33
+ export const keccakFromString = function ( a : string , bits : number = 256 ) {
34
+ assertIsString ( a )
35
+ const buf = Buffer . from ( a , 'utf8' )
36
+ return keccak ( buf , bits )
37
+ }
38
+
39
+ /**
40
+ * Creates Keccak hash of an 0x-prefixed string input
41
+ * @param a The input data (String)
42
+ * @param bits (number = 256) The Keccak width
43
+ */
44
+ export const keccakFromHexString = function ( a : string , bits : number = 256 ) {
45
+ assertIsHexString ( a )
46
+ return keccak ( toBuffer ( a ) , bits )
47
+ }
48
+
49
+ /**
50
+ * Creates Keccak hash of a number array input
51
+ * @param a The input data (number[])
52
+ * @param bits (number = 256) The Keccak width
53
+ */
54
+ export const keccakFromArray = function ( a : number [ ] , bits : number = 256 ) {
55
+ assertIsArray ( a )
56
+ return keccak ( toBuffer ( a ) , bits )
57
+ }
58
+
59
+ /**
60
+ * Creates SHA256 hash of a Buffer input.
61
+ * @param a The input data (Buffer)
62
+ */
63
+ export const sha256 = function ( a : Buffer ) : Buffer {
64
+ assertIsBuffer ( a )
65
+ return _sha256 ( a )
66
+ }
67
+
68
+ /**
69
+ * Creates SHA256 hash of a string input.
70
+ * @param a The input data (string)
71
+ */
72
+ export const sha256FromString = function ( a : string ) : Buffer {
73
+ assertIsString ( a )
74
+ return _sha256 ( a )
75
+ }
76
+
77
+ /**
78
+ * Creates SHA256 hash of a number[] input.
79
+ * @param a The input data (number[])
80
+ */
81
+ export const sha256FromArray = function ( a : number [ ] ) : Buffer {
82
+ assertIsArray ( a )
83
+ return _sha256 ( a )
84
+ }
85
+
86
+ /**
87
+ * Creates SHA256 hash of an input.
88
+ * @param a The input data (Buffer|Array|String)
89
+ */
90
+ const _sha256 = function ( a : any ) : Buffer {
40
91
a = toBuffer ( a )
41
92
return createHash ( 'sha256' )
42
93
. update ( a )
43
94
. digest ( )
44
95
}
45
96
97
+ /**
98
+ * Creates RIPEMD160 hash of a Buffer input.
99
+ * @param a The input data (Buffer)
100
+ * @param padded Whether it should be padded to 256 bits or not
101
+ */
102
+ export const ripemd160 = function ( a : Buffer , padded : boolean ) : Buffer {
103
+ assertIsBuffer ( a )
104
+ return _ripemd160 ( a , padded )
105
+ }
106
+
107
+ /**
108
+ * Creates RIPEMD160 hash of a string input.
109
+ * @param a The input data (String)
110
+ * @param padded Whether it should be padded to 256 bits or not
111
+ */
112
+ export const ripemd160FromString = function ( a : string , padded : boolean ) : Buffer {
113
+ assertIsString ( a )
114
+ return _ripemd160 ( a , padded )
115
+ }
116
+
117
+ /**
118
+ * Creates RIPEMD160 hash of a number[] input.
119
+ * @param a The input data (number[])
120
+ * @param padded Whether it should be padded to 256 bits or not
121
+ */
122
+ export const ripemd160FromArray = function ( a : number [ ] , padded : boolean ) : Buffer {
123
+ assertIsArray ( a )
124
+ return _ripemd160 ( a , padded )
125
+ }
126
+
46
127
/**
47
128
* Creates RIPEMD160 hash of the input.
48
129
* @param a The input data (Buffer|Array|String|Number)
49
130
* @param padded Whether it should be padded to 256 bits or not
50
131
*/
51
- export const ripemd160 = function ( a : any , padded : boolean ) : Buffer {
132
+ const _ripemd160 = function ( a : any , padded : boolean ) : Buffer {
52
133
a = toBuffer ( a )
53
134
const hash = createHash ( 'rmd160' )
54
135
. update ( a )
0 commit comments