Skip to content

Commit d86b990

Browse files
committed
- Refactored Crypto import
- substr() => substring() - Added randStr()
1 parent 77d1687 commit d86b990

File tree

1 file changed

+19
-9
lines changed

1 file changed

+19
-9
lines changed

api/helper.js

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ Refs:
33
https://upstash.com/docs/redis/sdks/ts/pipelining/pipeline-transaction
44
https://upstash.com/docs/redis/sdks/ts/pipelining/auto-pipeline
55
*/
6-
import Crypto from 'node:crypto';
6+
import { hash as cryptoHash, createHmac, getRandomValues, randomUUID } from 'node:crypto';
7+
import { Buffer } from "node:buffer";
78
import { Redis } from '@upstash/redis';
89

910
const secret = process.env.SECRET;
@@ -33,22 +34,31 @@ const redisRateLimit = new Redis({
3334
})
3435

3536
function hash(str){
36-
return Crypto.hash('md5', str, 'base64url').substr(0,hashLen);
37-
// For small size str Crypto.hash() is faster than Crypto.createHash()
37+
return cryptoHash('md5', str, 'base64url').substring(0,hashLen);
38+
// For small size str crypto.hash() is faster than crypto.createHash()
39+
// Ref: https://nodejs.org/api/crypto.html#cryptohashalgorithm-data-outputencoding
3840
}
3941

4042
function sign(str){
41-
// Note: https://nodejs.org/api/crypto.html#using-strings-as-inputs-to-cryptographic-apis
42-
return Crypto.createHmac('md5', secret).update(str).digest('base64url').substr(0,sigLen);
43+
// Ref: https://nodejs.org/api/crypto.html#using-strings-as-inputs-to-cryptographic-apis
44+
return createHmac('md5', secret).update(str).digest('base64url').substring(0,sigLen);
45+
}
46+
47+
//Brief: Return random base64url string of given length
48+
function randStr(len = hashLen){
49+
const byteSize = Math.ceil(len*6/8);
50+
const buff = Buffer.alloc(byteSize);
51+
getRandomValues(buff);
52+
return buff.toString('base64url').substring(0,len);
4353
}
4454

4555
export function id(){
4656
return sign('id');
4757
}
4858

4959
export function validate(key){
50-
const sig = key.substr(0, sigLen);
51-
const hash = key.substr(sigLen,);
60+
const sig = key.substring(0, sigLen);
61+
const hash = key.substring(sigLen);
5262
if (sig === sign(hash + 'public')){
5363
return 'public';
5464
} else if (sig === sign(hash + 'private')){
@@ -61,7 +71,7 @@ export function validate(key){
6171
export function genPublicKey(privateOrPublicKey){
6272
if (validate(privateOrPublicKey) === 'public') return privateOrPublicKey;
6373
const privateKey = privateOrPublicKey;
64-
const privateHash = privateKey.substr(sigLen,);
74+
const privateHash = privateKey.substring(sigLen);
6575
const publicHash = hash(privateHash);
6676
const publicKey = sign(publicHash + 'public') + publicHash;
6777
return publicKey;
@@ -90,7 +100,7 @@ export function cacheDel(privateOrPublicKey, key){
90100
return redisRateLimit.hdel(dbKey, key);
91101
}
92102

93-
export function genKeyPair(seed = Crypto.randomUUID()){
103+
export function genKeyPair(seed = randomUUID()){
94104
const privateHash = hash(seed);
95105
const privateKey = sign(privateHash + 'private') + privateHash;
96106
const publicKey = genPublicKey(privateKey);

0 commit comments

Comments
 (0)