File tree Expand file tree Collapse file tree 2 files changed +26
-2
lines changed Expand file tree Collapse file tree 2 files changed +26
-2
lines changed Original file line number Diff line number Diff line change @@ -19,13 +19,26 @@ const NIST = {
19
19
minIterationCount : 1000
20
20
}
21
21
22
+ /**
23
+ * Maps an IPFS hash name to its forge equivalent.
24
+ *
25
+ * See https://github.com/multiformats/multihash/blob/master/hashtable.csv
26
+ *
27
+ * @private
28
+ */
29
+ const hashName2Forge = {
30
+ 'sha1' : 'sha1' ,
31
+ 'sha2-256' : 'sha256' ,
32
+ 'sha2-512' : 'sha512' ,
33
+
34
+ }
22
35
const defaultOptions = {
23
36
// See https://cryptosense.com/parametesr-choice-for-pbkdf2/
24
37
dek : {
25
38
keyLength : 512 / 8 ,
26
39
iterationCount : 10000 ,
27
40
salt : 'you should override this value with a crypto secure random number' ,
28
- hash : 'sha512 '
41
+ hash : 'sha2-512 '
29
42
}
30
43
}
31
44
@@ -120,13 +133,18 @@ class Keychain {
120
133
}
121
134
this . dek = opts . dek
122
135
136
+ // Get the hashing alogorithm
137
+ const hashAlgorithm = hashName2Forge [ opts . dek . hash ]
138
+ if ( ! hashAlgorithm )
139
+ throw new Error ( `dek.hash '${ opts . dek . hash } ' is unknown or not supported` )
140
+
123
141
// Create the derived encrypting key
124
142
let dek = forge . pkcs5 . pbkdf2 (
125
143
opts . passPhrase ,
126
144
opts . dek . salt ,
127
145
opts . dek . iterationCount ,
128
146
opts . dek . keyLength ,
129
- opts . dek . hash )
147
+ hashAlgorithm )
130
148
dek = forge . util . bytesToHex ( dek )
131
149
Object . defineProperty ( this , '_' , { value : ( ) => dek } )
132
150
Original file line number Diff line number Diff line change @@ -41,6 +41,12 @@ module.exports = (datastore1, datastore2) => {
41
41
expect ( Keychain . options ) . to . exist ( )
42
42
} )
43
43
44
+ it ( 'needs a supported hashing alorithm' , ( ) => {
45
+ const ok = new Keychain ( datastore2 , { passPhrase : passPhrase , dek : { hash : 'sha2-256' } } )
46
+ expect ( ok ) . to . exist ( )
47
+ expect ( ( ) => new Keychain ( datastore2 , { passPhrase : passPhrase , dek : { hash : 'my-hash' } } ) ) . to . throw ( )
48
+ } )
49
+
44
50
describe ( 'key name' , ( ) => {
45
51
it ( 'is a valid filename and non-ASCII' , ( ) => {
46
52
ks . removeKey ( '../../nasty' , ( err ) => {
You can’t perform that action at this time.
0 commit comments