-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathQueueITHelpers.ts
More file actions
152 lines (136 loc) · 5.01 KB
/
QueueITHelpers.ts
File metadata and controls
152 lines (136 loc) · 5.01 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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
import {Base64} from "./helpers/Base64";
import {ModeOfOperationCBC, Utf8Converter} from "./helpers/Aes";
import {md5} from "./helpers/Md5";
import sha256 from "./helpers/Sha";
export class Utils {
static maxDate(): Date {
return new Date(Date.UTC(9999, 12 - 1, 31, 23, 59, 59, 999));
}
static utcNow(): number {
const now = new Date();
return Date.UTC(now.getUTCFullYear(), now.getUTCMonth(), now.getUTCDate(),
now.getUTCHours(), now.getUTCMinutes(), now.getUTCSeconds(), now.getUTCMilliseconds());
}
static padRight(str: string, padding: string, stringSize: number) {
while (str.length < stringSize) {
str += padding
}
return str;
}
// Based on REF 4122 section 4.4 http://www.ietf.org/rfc/rfc4122.txt
static generateUUID(): string {
const s = [];
// eslint-disable-next-line no-secrets/no-secrets
const hexDigits = "0123456789abcdef";
for (let i = 0; i < 36; i++) {
s[i] = hexDigits.substr(Math.floor(Math.random() * 0x10), 1);
}
s[14] = "4"; // bits 12-15 of the time_hi_and_version field to 0010
s[19] = hexDigits.substr((s[19] & 0x3) | 0x8, 1); // bits 6-7 of the clock_seq_hi_and_reserved to 01
s[8] = s[13] = s[18] = s[23] = "-";
return s.join("");
}
static generateKey(value: string): Uint8Array {
const utf8Bytes = Utf8Converter.toBytes(value);
return sha256(utf8Bytes);
}
static generateIV(value: string): Uint8Array {
const utf8Bytes = Utf8Converter.toBytes(value);
let bytes: Uint8Array = md5(utf8Bytes);
if(bytes.slice){
return bytes.slice(0, 16);
}else{
return new Uint8Array(bytes.buffer.slice(0, 16));
}
}
static uint8ArrayToHexString(byteArray: Uint8Array): string {
let acc = '';
for (let i = 0; i < byteArray.length; i++) {
let val = byteArray[i];
acc += ('0' + val.toString(16)).slice(-2);
}
return acc;
}
static uint8ArrayToString(array: Uint8Array): string {
let out = "", i: number, len: number , c: number;
let char2: number, char3: number;
len = array.length;
i = 0;
while (i < len) {
c = array[i++];
switch (c >> 4) {
case 0:
case 1:
case 2:
case 3:
case 4:
case 5:
case 6:
case 7:
// 0xxxxxxx
out += String.fromCharCode(c);
break;
case 12:
case 13:
// 110x xxxx 10xx xxxx
char2 = array[i++];
out += String.fromCharCode(((c & 0x1F) << 6) | (char2 & 0x3F));
break;
case 14:
// 1110 xxxx 10xx xxxx 10xx xxxx
char2 = array[i++];
char3 = array[i++];
out += String.fromCharCode(((c & 0x0F) << 12) |
((char2 & 0x3F) << 6) |
((char3 & 0x3F) << 0));
break;
}
}
return out;
}
static stringToUint8Array(value: string): Uint8Array {
const encoded = encodeURIComponent(value);
const bytes = [];
let state = 0;
for (let i = 0; i < encoded.length; i++) {
switch (state) {
case 0: //Convert chars to bytes
if (encoded[i] == '%') {
state = 1;
} else {
bytes.push(encoded.charCodeAt(i));
}
break;
case 1: //Seen '%'
state = 2;
break;
case 2: // Seen %H
bytes.push(parseInt(encoded.substring(i - 1, i + 1), 16));
state = 0;
break;
}
}
return new Uint8Array(bytes);
}
}
export class ShaHashing {
public static GenerateHash(secretKey: string, tokenString: string): Uint8Array {
const content = Utf8Converter.toBytes(tokenString + secretKey);
return sha256(content);
}
}
export class AESEncryption {
static EncryptPayload(secretKey: string, tokenIdentifier: string, valueToEncrypt: string): string {
const key = Utils.generateKey(secretKey);
const iv = Utils.generateIV(tokenIdentifier);
const aesCBC = new ModeOfOperationCBC(key, iv)
const encrypted: Uint8Array = aesCBC.encrypt(valueToEncrypt);
return Base64.encode(encrypted);
}
static DecryptPayload(secretKey: string, tokenIdentifier: string, valueToDecrypt: Uint8Array): Uint8Array {
const key = Utils.generateKey(secretKey);
const iv = Utils.generateIV(tokenIdentifier);
const aesCBC = new ModeOfOperationCBC(key, iv);
return aesCBC.decrypt(valueToDecrypt);
}
}