chatlocal / static /js /SecurityHandler.js
MGLDZM's picture
dev step
9a8d759
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
}
}