-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathCryptoUtilities.js
More file actions
47 lines (38 loc) · 1.25 KB
/
CryptoUtilities.js
File metadata and controls
47 lines (38 loc) · 1.25 KB
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
/*
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License.
*/
const { v4: uuidv4 } = require('uuid');
/**
* Basic cryptography methods for generating GUIDs and encoding state.
* Source: https://github.com/AzureAD/microsoft-authentication-library-for-js/tree/dev/lib/msal-node/src/crypto
*/
class CryptoUtilities {
static base64Encode(str, encoding) {
return Buffer.from(str, encoding).toString("base64");
}
static base64EncodeUrl(str, encoding) {
return this.base64Encode(str, encoding)
.replace(/=/g, "")
.replace(/\+/g, "-")
.replace(/\//g, "_");
}
static base64Decode(base64Str) {
return Buffer.from(base64Str, "base64").toString("utf8");
}
static base64DecodeUrl(base64Str) {
let str = base64Str.replace(/-/g, "+").replace(/_/g, "/");
while (str.length % 4) {
str += "=";
}
return this.base64Decode(str);
}
static generateGuid() {
return uuidv4();
}
static isGuid(guid) {
const regexGuid = /^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i;
return regexGuid.test(guid);
}
}
module.exports = CryptoUtilities;