This repository has been archived by the owner on Apr 11, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 387
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use random bytes when generating nonce
- Loading branch information
1 parent
977c79f
commit e0e080c
Showing
2 changed files
with
10 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,14 @@ | ||
import crypto from 'crypto'; | ||
|
||
export default function nonce(): string { | ||
const length = 15; | ||
let nonce = ''; | ||
const bytes = crypto.randomBytes(length); | ||
|
||
for (let i = 0; i <= 3; i++) { | ||
nonce += Math.round(Number(new Date()) * Math.random()); | ||
} | ||
const nonce = bytes | ||
.map((byte) => { | ||
return byte % 10; | ||
}) | ||
.join(''); | ||
|
||
const str = nonce.substr(nonce.length - length); | ||
return str; | ||
return nonce; | ||
} |