-
Notifications
You must be signed in to change notification settings - Fork 29
/
application.ts
48 lines (41 loc) · 1.26 KB
/
application.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
import { CryptoEngine, setEngine } from "pkijs";
import { XE, XmlError } from "xml-core";
export interface CryptoEx extends Crypto {
name: string;
}
let engineCrypto: CryptoEx | null = null;
export class Application {
/**
* Sets crypto engine for the current Application
* @param {string} name
* @param {Crypto} crypto
* @returns void
*/
public static setEngine(name: string, crypto: Crypto): void {
engineCrypto = {
getRandomValues: crypto.getRandomValues.bind(crypto),
subtle: crypto.subtle,
name,
};
setEngine(name, new CryptoEngine({ name, crypto, subtle: crypto.subtle }), new CryptoEngine({ name, crypto, subtle: crypto.subtle }));
}
/**
* Gets the crypto module from the Application
*/
public static get crypto(): CryptoEx {
if (!engineCrypto) {
throw new XmlError(XE.CRYPTOGRAPHIC_NO_MODULE);
}
return engineCrypto;
}
public static isNodePlugin(): boolean {
return (typeof self === "undefined" && typeof window === "undefined");
}
}
// set default w3 WebCrypto
function init() {
if (!Application.isNodePlugin()) {
Application.setEngine("W3 WebCrypto module", self.crypto);
}
}
init();