-
Notifications
You must be signed in to change notification settings - Fork 242
/
auth_apple_nonce_node.js
28 lines (24 loc) · 863 Bytes
/
auth_apple_nonce_node.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
// This snippet file was generated by processing the source file:
// ./auth-next/apple.js
//
// To update the snippets in this file, edit the source and then run
// 'npm run snippets'.
// [START auth_apple_nonce_node_modular]
const crypto = require("crypto");
const string_decoder = require("string_decoder");
// Generate a new random string for each sign-in
const generateNonce = (length) => {
const decoder = new string_decoder.StringDecoder("ascii");
const buf = Buffer.alloc(length);
let nonce = "";
while (nonce.length < length) {
crypto.randomFillSync(buf);
nonce = decoder.write(buf);
}
return nonce.slice(0, length);
};
const unhashedNonce = generateNonce(10);
// SHA256-hashed nonce in hex
const hashedNonceHex = crypto.createHash('sha256')
.update(unhashedNonce).digest().toString('hex');
// [END auth_apple_nonce_node_modular]