-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerateTokens.js
34 lines (25 loc) · 946 Bytes
/
generateTokens.js
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
const crypto = require('crypto');
const btoa=require('btoa');
function base64UrlEncode(byteArray) {
const charCodes = String.fromCharCode(...byteArray);
return btoa(charCodes)
.replace(/\+/g, '-')
.replace(/\//g, '_')
.replace(/=/g, '');
}
async function sha256(verifier) {
const msgBuffer = new TextEncoder('utf-8').encode(verifier);
// hash the message
const hashBuffer = await crypto.subtle.digest('SHA-256', msgBuffer);
return new Uint8Array(hashBuffer);
}
async function generateVerifierChallengePair() {
const randomBytes = crypto.getRandomValues(new Uint8Array(32));
const verifier = base64UrlEncode(randomBytes);
console.log('g Verifier:', verifier);
const challenge = await sha256(verifier).then(base64UrlEncode);
console.log('g Challenge:', challenge)
return [verifier, challenge];
}
module.exports.generateTokens=generateVerifierChallengePair;
//console.log(module)