-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaes-gcp.ts
More file actions
158 lines (126 loc) · 3.84 KB
/
aes-gcp.ts
File metadata and controls
158 lines (126 loc) · 3.84 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
153
154
155
156
157
158
/**
* Inspired by Chris Veness (MIT Licence). See:
* https://gist.github.com/chrisveness/43bcda93af9f646d083fad678071b90a
*/
import { StrategyHandler, EncryptionContext } from '../handler';
/**
* WebCryptoAesGcp Context
*/
export interface WebCryptoAesGcpContext extends EncryptionContext {
password: string;
}
/**
* Defines a WebCrypto Algorithm
*/
export interface WebCryptoAlgorithm {
iv: Uint8Array,
name: string
}
/**
* Valid key usages
*/
export enum KeyUsages {
ENCRYPT = 'encrypt',
DECRYPT = 'decrypt',
}
/**
* PGP KeypairHandler Handler
*/
export class WebCryptoAesGcpHandler extends StrategyHandler<WebCryptoAesGcpContext> {
/**
* Name of the algorithm
*/
static algorithmName = 'AES-GCM';
/**
* Returns a Crypto instance.
* This is corresponds to window.crypto
* in the browser and to a mock library in nodejs
*/
get crypto(): Crypto {
return crypto;
}
/**
* Returns an IV
*/
getIV(): Uint8Array {
return this.crypto.getRandomValues(new Uint8Array(12));
}
/**
* Generates the algorithm type
* to be used in the encrpytion method.
*
* If `iv` is not passed
* a new `iv` is generated
*
* @param iv?
*/
getAlgorithm(iv?: Uint8Array): WebCryptoAlgorithm {
const algorithm = {
iv: iv || this.getIV(),
name: (this.constructor as typeof WebCryptoAesGcpHandler).algorithmName,
};
return algorithm;
}
/**
* Returns an encryption key
*
* @param algorithm
* @param password
* @param usages
*/
async getKey(algorithm: WebCryptoAlgorithm, password: string, usages: KeyUsages[]): Promise<CryptoKey> {
const encoded = this.encoder.encode(password);
const hashed = await crypto.subtle.digest({ name: 'SHA-256' }, encoded);
const key = await this.crypto.subtle.importKey('raw', hashed, algorithm, false, usages);
return key;
}
/**
* Encrypts plaintext using AES-GCM
* with the supplied password
*/
async encrypt(context: WebCryptoAesGcpContext, plaintext: string): Promise<string> {
const alg = this.getAlgorithm();
const key = await this.getKey(alg, context.password, [KeyUsages.ENCRYPT]);
// encode plaintext as UTF-8
const encoded = this.encoder.encode(plaintext);
// encrypt plaintext using key
const buffer = await crypto.subtle.encrypt(alg, key, encoded);
// transform as byte array
const bytes = Array.from(new Uint8Array(buffer));
// encode in base64
const encryptedb64 = btoa(bytes.map(byte => String.fromCharCode(byte)).join(''));
// Generate ivHex
const ivHex = Array.from(alg.iv).map(b => ('00' + b.toString(16)).slice(-2)).join('');
// Final result of the encryption
const encrypted = ivHex + encryptedb64;
return encrypted;
}
/**
* Decrypts an encrypted string using AES-GCM
* with the supplied password
*
* @param context
* @param encrypted
*/
async decrypt(context: WebCryptoAesGcpContext, encrypted: string): Promise<string> {
const ivSlice = encrypted.slice(0, 24).match(/.{2}/g);
if (!ivSlice) {
throw new Error('Could not extract IV from encrypted string');
}
const iv = new Uint8Array(ivSlice.map((byte) => parseInt(byte, 16)));
const alg = this.getAlgorithm(iv);
const key = await this.getKey(alg, context.password, [KeyUsages.DECRYPT]);
const decoded = atob(encrypted.slice(24)).match(/[\s\S]/g);
if (!decoded) {
throw new Error('Could not decode encrypted string');
}
// ciphertext as Uint8Array
// note: why doesn't ctUint8 = new TextEncoder().encode(ctStr) work?
const bytes = new Uint8Array(decoded.map((char) => char.charCodeAt(0)));
// decrypt ciphertext using key
const buffer = await this.crypto.subtle.decrypt(alg, key, bytes);
// decode password from UTF-8
const decrypted = this.decoder.decode(buffer);
return decrypted;
}
}