File size: 1,635 Bytes
039afa2
 
 
 
1f6fba1
039afa2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1f6fba1
 
039afa2
 
 
 
 
 
 
 
 
 
 
 
9a8d759
039afa2
 
 
 
 
 
1f6fba1
039afa2
 
 
 
 
 
 
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
class Security{
    keypair;
    privateKey;
    publicKey;
    fingerprint;
    ready = false;

    async start(){
        let keypair = await window.crypto.subtle.generateKey(
            {
                name: "RSA-PSS",
                modulusLength: 1024, 
                publicExponent: new Uint8Array([0x01, 0x00, 0x01]),
                hash: {name: "SHA-256"}, 
            },
            false, 
            ["sign"] 
        )
        this.keypair = keypair
        this.privateKey = keypair.privateKey
        this.publicKey = keypair.publicKey
        
        let exportKey_ba = await window.crypto.subtle.exportKey(
            "spki",
            keypair.publicKey
        )
        
        let exportKey_u8 = new Uint8Array(exportKey_ba);
        this.publicKey = "-----BEGIN PUBLIC KEY-----\n"+btoa(String.fromCharCode(...exportKey_u8))+"\n-----END PUBLIC KEY-----"

        this.fingerprint = await this.generateFingerprint()

        this.ready = true
        return true
        
    }

    async sign(data){
        let encoder = new TextEncoder()
        let sign_ba = await window.crypto.subtle.sign(
            {
                name: "RSA-PSS",
                saltLength: 32, 
            },
            this.keypair.privateKey,
            encoder.encode(data)
        )
        let sign_u8 = new Uint8Array(sign_ba);
        return btoa(String.fromCharCode(...sign_u8))
    }

    async generateFingerprint(){
        const fpPromise = await import('/static/js/fingerprintv4.js').then(FingerprintJS => FingerprintJS.load())
        const fp = await fpPromise.get()
        return fp.visitorId
    }


}