/
demo.view.ts
76 lines (65 loc) · 1.89 KB
/
demo.view.ts
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
import { Component, OnInit } from '@angular/core';
import {
Aenigma,
AenigmaUtil,
Credentials,
EncryptedValue,
AenigmaStorageService,
AenigmaLocalStorageProvider
} from '../src';
@Component({
selector: 'aenigma-demo-view',
templateUrl: './demo/demo.view.html'
})
export class DemoViewComponent {
private static _CREDENTIALS_JWK_KEY: string = 'AENIGMA_CREDENTIALS_JWK';
private _aenigma: Aenigma;
private _encryptedStorage: AenigmaStorageService;
private _jwk: string;
private _plaintext: string;
private _encrypted: string;
constructor(private _encryptedStorageProvider: AenigmaLocalStorageProvider) {
const jwk: any = localStorage.getItem(
DemoViewComponent._CREDENTIALS_JWK_KEY
);
if (!jwk) {
this.generate();
} else {
Credentials.import(
JSON.parse(jwk)
).subscribe((credentials: Credentials) => {
this.generate(credentials);
});
}
}
public generate(credentials?: Credentials): void {
Aenigma.create(credentials).subscribe(
(aenigma: Aenigma) => {
localStorage.setItem(
DemoViewComponent._CREDENTIALS_JWK_KEY,
JSON.stringify(aenigma.credentials.key)
);
this._jwk = JSON.stringify(aenigma.credentials.key);
this._aenigma = aenigma;
this._encryptedStorage = new AenigmaStorageService(
this._aenigma,
this._encryptedStorageProvider
);
},
error => {
console.error(error);
}
);
}
public encrypt(plaintext: string): void {
this._aenigma.encrypt(plaintext).subscribe((encrypted: EncryptedValue) => {
this._encrypted = encrypted.stringify();
});
}
public decrypt(ciphertext: string): void {
const encrypted: EncryptedValue = EncryptedValue.parse(ciphertext);
this._aenigma.decrypt(encrypted).subscribe((decrypted: string) => {
this._plaintext = decrypted;
});
}
}