diff options
| author | MeiMei <30769358+mei23@users.noreply.github.com> | 2020-03-29 23:16:36 +0900 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2020-03-29 23:16:36 +0900 |
| commit | 244ef0cb8f82b18c22990ece728f2e1fe8398a62 (patch) | |
| tree | 781b5820207bf78d831f13928d5b5253582436b0 /src/misc | |
| parent | 12.28.0 (diff) | |
| download | sharkey-244ef0cb8f82b18c22990ece728f2e1fe8398a62.tar.gz sharkey-244ef0cb8f82b18c22990ece728f2e1fe8398a62.tar.bz2 sharkey-244ef0cb8f82b18c22990ece728f2e1fe8398a62.zip | |
トークン系の乱数ソースではcryptoを使うように (#6200)
Diffstat (limited to 'src/misc')
| -rw-r--r-- | src/misc/secure-rndstr.ts | 21 |
1 files changed, 21 insertions, 0 deletions
diff --git a/src/misc/secure-rndstr.ts b/src/misc/secure-rndstr.ts new file mode 100644 index 0000000000..76ee1225eb --- /dev/null +++ b/src/misc/secure-rndstr.ts @@ -0,0 +1,21 @@ +import * as crypto from 'crypto'; + +const L_CHARS = '0123456789abcdefghijklmnopqrstuvwxyz'; +const LU_CHARS = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; + +export function secureRndstr(length = 32, useLU = true): string { + const chars = useLU ? LU_CHARS : L_CHARS; + const chars_len = chars.length; + + let str = ''; + + for (let i = 0; i < length; i++) { + let rand = Math.floor((crypto.randomBytes(1).readUInt8(0) / 0xFF) * chars_len); + if (rand === chars_len) { + rand = chars_len - 1; + } + str += chars.charAt(rand); + } + + return str; +} |