Newer
Older
import { AESGCMOutput, ICryptoService, RSAKeys } from "./ICryptoService";
import { base64ToArrayBuffer } from "../../utils/common";

Alexey Lunin
committed
const exportKey = async (key: CryptoKey): Promise<ArrayBuffer> => {
return crypto.subtle.exportKey("raw", key);

Alexey Lunin
committed
};
const convertPemToBinary = (pem: string) => {
const lines = pem.split("\n");
let encoded = "";
for (let i = 0; i < lines.length; i++) {
if (
lines[i].trim().length > 0 &&
lines[i].indexOf("-BEGIN PRIVATE KEY-") < 0 &&
lines[i].indexOf("-BEGIN PUBLIC KEY-") < 0 &&
lines[i].indexOf("-END PRIVATE KEY-") < 0 &&
lines[i].indexOf("-END PUBLIC KEY-") < 0
) {
encoded += lines[i].trim();
}
}
return base64ToArrayBuffer(encoded);
};
const getBytes = (value: string | ArrayBuffer, encoding): ArrayBuffer => {
let bytes;
if (typeof value === "string") {
if (encoding === "base64") {
bytes = base64ToArrayBuffer(value);
} else {
const encoder = new TextEncoder();
bytes = encoder.encode(value);
}
} else {
bytes = value;
}

Alexey Lunin
committed
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
class CryptoServiceWeb implements ICryptoService {
public async encryptAESGCM(data: string): Promise<AESGCMOutput>;
public async encryptAESGCM(data: ArrayBuffer): Promise<AESGCMOutput>;
public async encryptAESGCM(
data: string | ArrayBuffer
): Promise<AESGCMOutput> {
const key = await crypto.subtle.generateKey(
{
name: "AES-GCM",
length: 256,
},
true,
["encrypt", "decrypt"]
);
let encoded;
if (typeof data === "string") {
encoded = new TextEncoder().encode(data);
} else {
encoded = data;
}
const iv = crypto.getRandomValues(new Buffer(12));
const encrypted = await crypto.subtle.encrypt(
{ name: "AES-GCM", iv: iv },
key,
encoded
);
return { data: encrypted, key: await exportKey(key), iv };
}
public async decryptAESGCM(
data: ArrayBuffer,
key: ArrayBuffer,
iv: ArrayBuffer
): Promise<string>;
public async decryptAESGCM(
data: ArrayBuffer,
key: ArrayBuffer,
iv: ArrayBuffer,
returnBuffer: true
): Promise<ArrayBuffer>;
public async decryptAESGCM(
data: ArrayBuffer,
key: ArrayBuffer,
iv: ArrayBuffer,
returnBuffer?: boolean
): Promise<string | ArrayBuffer> {
const importedKey = await crypto.subtle.importKey(
"raw",
key,
{
name: "AES-GCM",
length: 256,
},
true,
["encrypt", "decrypt"]
);
const decrypted = await crypto.subtle.decrypt(
{ name: "AES-GCM", iv: iv },
importedKey,
data
);

Alexey Lunin
committed
if (returnBuffer) {
return decrypted;
}

Alexey Lunin
committed
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
return new TextDecoder().decode(decrypted);
}
public async verifyRSASignature(
publicKeyPEM: string,
data: ArrayBuffer,
signature: ArrayBuffer
): Promise<boolean> {
const publicKey = await crypto.subtle.importKey(
"spki",
convertPemToBinary(publicKeyPEM),
{
name: "RSASSA-PKCS1-v1_5",
hash: "SHA-256",
},
true,
["verify"]
);
return await crypto.subtle.verify(
{
name: "RSASSA-PKCS1-v1_5",
hash: "SHA-256",
},
publicKey,
signature,
data
);
}
public async generateRSAKeys(): Promise<RSAKeys> {
throw new Error("The function is not implemented");
}
public async encryptRSA(
publicKeyPEM: string,
data: ArrayBuffer
): Promise<ArrayBuffer> {
const publicKey = await crypto.subtle.importKey(
"spki",
convertPemToBinary(publicKeyPEM),
{
name: "RSA-OAEP",
hash: "SHA-256",
},
true,
["encrypt"]
);
return crypto.subtle.encrypt(
{
name: "RSA-OAEP",
},
publicKey,
data
);
}
public async decryptRSA(
privateKeyPEM: string,
data: ArrayBuffer
): Promise<ArrayBuffer> {
throw new Error("The function is not implemented");
}
public async signRSA(
privateKeyPEM: string,
data: ArrayBuffer
): Promise<ArrayBuffer> {
throw new Error("The function is not implemented");
}
public async SHA1(
value: string | ArrayBuffer,
encoding = "utf8"
): Promise<ArrayBuffer> {
const bytes = getBytes(value, encoding);
return await crypto.subtle.digest("SHA-1", bytes);
}
public async SHA256(
value: string | ArrayBuffer,
encoding = "utf8"
): Promise<ArrayBuffer> {
const bytes = getBytes(value, encoding);
return await crypto.subtle.digest("SHA-256", bytes);
}
public async SHA384(
value: string | ArrayBuffer,
encoding = "utf8"
): Promise<ArrayBuffer> {
const bytes = getBytes(value, encoding);
return await crypto.subtle.digest("SHA-384", bytes);
}
public async SHA512(
value: string | ArrayBuffer,
encoding = "utf8"
): Promise<ArrayBuffer> {
const bytes = getBytes(value, encoding);
return await crypto.subtle.digest("SHA-512", bytes);
}
public async MD5(
value: string | ArrayBuffer,
encoding = "utf8"
): Promise<ArrayBuffer> {
const bytes = getBytes(value, encoding);
return md5.arrayBuffer(bytes);
}
}

Alexey Lunin
committed
export default CryptoServiceWeb;