FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
node-argon2/argon2.cjs at master · ranisalt/node-argon2 · GitHub
ranisalt
/
node-argon2
Public
Uh oh!
There was an error while loading.
Please reload this page
.
Notifications
You must be signed in to change notification settings
Fork
100
Star
2.2k
Code
Issues
3
Pull requests
6
Actions
Wiki
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
node-argon2
/
argon2.cjs
Copy path
More file actions
More file actions
Latest commit
History
History
History
204 lines (177 loc) · 5.05 KB
Breadcrumbs
node-argon2
/
argon2.cjs
Copy path
File metadata and controls
204 lines (177 loc) · 5.05 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
const
{
randomBytes
,
timingSafeEqual
}
=
require
(
"node:crypto"
)
;
const
{
promisify
}
=
require
(
"node:util"
)
;
const
{
deserialize
,
serialize
}
=
require
(
"@phc/format"
)
;
const
gypBuild
=
require
(
"node-gyp-build"
)
;
const
{
hash
:
bindingsHash
}
=
gypBuild
(
__dirname
)
;
/**
@type
{
(size: number) => Promise<Buffer>
} */
const
generateSalt
=
promisify
(
randomBytes
)
;
const
argon2d
=
0
;
const
argon2i
=
1
;
const
argon2id
=
2
;
module
.
exports
.
argon2d
=
argon2d
;
module
.
exports
.
argon2i
=
argon2i
;
module
.
exports
.
argon2id
=
argon2id
;
/**
@enum
{argon2i | argon2d | argon2id} */
const
types
=
Object
.
freeze
(
{
argon2d
,
argon2i
,
argon2id
}
)
;
/**
@enum
{'argon2d' | 'argon2i' | 'argon2id'} */
const
names
=
Object
.
freeze
(
{
[
types
.
argon2d
]
:
"argon2d"
,
[
types
.
argon2i
]
:
"argon2i"
,
[
types
.
argon2id
]
:
"argon2id"
,
}
)
;
const
defaults
=
{
hashLength
:
32
,
memoryCost
:
1
<<
16
,
parallelism
:
4
,
timeCost
:
3
,
type
:
argon2id
,
version
:
0x13
,
}
;
/**
*
@typedef
{
Object
} HashOptions
*
@property
{
number
} [hashLength=32]
*
@property
{
number
} [timeCost=3]
*
@property
{
number
} [memoryCost=65536]
*
@property
{
number
} [parallelism=4]
*
@property
{
keyof typeof names
} [type=argon2id]
*
@property
{
number
} [version=19]
*
@property
{
Buffer
} [salt]
*
@property
{
Buffer
} [associatedData]
*
@property
{
Buffer
} [secret]
*/
/**
* Hashes a password with Argon2, producing a raw hash
*
*
@overload
*
@param
{
Buffer | string
} password The plaintext password to be hashed
*
@param
{
HashOptions & { raw: true }
} options The parameters for Argon2
*
@returns
{
Promise<Buffer>
} The raw hash generated from `password`
*/
/**
* Hashes a password with Argon2, producing an encoded hash
*
*
@overload
*
@param
{
Buffer | string
} password The plaintext password to be hashed
*
@param
{
HashOptions & { raw?: boolean }
} [options] The parameters for Argon2
*
@returns
{
Promise<string>
} The encoded hash generated from `password`
*/
/**
*
@param
{
Buffer | string
} password The plaintext password to be hashed
*
@param
{
HashOptions & { raw?: boolean }
} [options] The parameters for Argon2
*/
async
function
hash
(
password
,
options
)
{
const
{
raw
,
salt
:
_salt
,
...
rest
}
=
{
...
defaults
,
...
options
}
;
if
(
rest
.
hashLength
>
2
**
32
-
1
)
{
throw
new
RangeError
(
"Hash length is too large"
)
;
}
if
(
rest
.
memoryCost
>
2
**
32
-
1
)
{
throw
new
RangeError
(
"Memory cost is too large"
)
;
}
if
(
rest
.
timeCost
>
2
**
32
-
1
)
{
throw
new
RangeError
(
"Time cost is too large"
)
;
}
if
(
rest
.
parallelism
>
2
**
24
-
1
)
{
throw
new
RangeError
(
"Parallelism is too large"
)
;
}
const
salt
=
_salt
??
(
await
generateSalt
(
16
)
)
;
const
{
hashLength
,
secret
=
Buffer
.
alloc
(
0
)
,
type
,
version
,
memoryCost
:
m
,
timeCost
:
t
,
parallelism
:
p
,
associatedData
:
data
=
Buffer
.
alloc
(
0
)
,
}
=
rest
;
const
result
=
await
bindingsHash
(
{
data
,
hashLength
,
m
,
p
,
password
:
Buffer
.
from
(
password
)
,
salt
,
secret
,
t
,
type
,
version
,
}
)
;
if
(
raw
)
{
return
result
;
}
const
params
=
{
m
,
p
,
t
}
;
if
(
data
.
byteLength
>
0
)
{
params
.
data
=
data
;
}
return
serialize
(
{
hash
:
result
,
id
:
names
[
type
]
,
params
,
salt
,
version
,
}
)
;
}
module
.
exports
.
hash
=
hash
;
/**
*
@param
{
string
} digest The digest to be checked
*
@param
{
Object
} [options] The current parameters for Argon2
*
@param
{
number
} [options.timeCost=3]
*
@param
{
number
} [options.memoryCost=65536]
*
@param
{
number
} [options.parallelism=4]
*
@param
{
number
} [options.version=0x13]
*
@returns
{
boolean
} `true` if the digest parameters do not match the parameters in `options`, otherwise `false`
*/
function
needsRehash
(
digest
,
options
=
{
}
)
{
const
{
memoryCost
,
timeCost
,
parallelism
,
version
}
=
{
...
defaults
,
...
options
,
}
;
const
{
version
:
v
,
params
:
{
m
,
t
,
p
}
,
}
=
deserialize
(
digest
)
;
return
(
Number
(
v
)
!==
Number
(
version
)
||
Number
(
m
)
!==
Number
(
memoryCost
)
||
Number
(
t
)
!==
Number
(
timeCost
)
||
Number
(
p
)
!==
Number
(
parallelism
)
)
;
}
module
.
exports
.
needsRehash
=
needsRehash
;
/**
*
@typedef
{
Object
} VerifyOptions
*
@property
{
Buffer
} [secret]
*/
/**
*
@param
{
string
} digest The digest to be checked
*
@param
{
Buffer | string
} password The plaintext password to be verified
*
@param
{
VerifyOptions
} [options] The current parameters for Argon2
*
@returns
{
Promise<boolean>
} `true` if the digest parameters matches the hash generated from `password`, otherwise `false`
*/
async
function
verify
(
digest
,
password
,
options
=
{
}
)
{
const
{
id
,
...
rest
}
=
deserialize
(
digest
)
;
if
(
!
(
id
in
types
)
)
{
return
false
;
}
const
{
version
=
0x10
,
params
:
{
m
,
t
,
p
,
data
=
""
}
,
salt
,
hash
:
actual
,
}
=
rest
;
const
{
secret
=
Buffer
.
alloc
(
0
)
}
=
options
;
const
expected
=
await
bindingsHash
(
{
data
:
Buffer
.
from
(
data
,
"base64"
)
,
hashLength
:
actual
.
byteLength
,
m
:
Number
(
m
)
,
p
:
Number
(
p
)
,
password
:
Buffer
.
from
(
password
)
,
salt
,
secret
,
t
:
Number
(
t
)
,
type
:
types
[
id
]
,
version
:
Number
(
version
)
,
}
)
;
return
timingSafeEqual
(
expected
,
actual
)
;
}
module
.
exports
.
verify
=
verify
;
Back
|
FazBrowse Home
|
New Git URL