-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
SourceMap disabled, declaration updated, encrypt
made module
- Loading branch information
Showing
6 changed files
with
119 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import { box, randomBytes } from 'tweetnacl'; | ||
import { decodeUTF8, encodeBase64 } from 'tweetnacl-util'; | ||
|
||
export class EncryptionPlugin { | ||
private _keyPair: nacl.BoxKeyPair | null = null; | ||
private _adminPanelPublicKey: Uint8Array | null = null; | ||
private _sharedKey: Uint8Array | null = null; | ||
|
||
public newNonce() { | ||
return randomBytes(box.nonceLength); | ||
}; | ||
|
||
public encryptData(json: any) { | ||
try { | ||
if (!this._sharedKey) | ||
throw new Error("Shared key not generated"); | ||
|
||
const nonce = this.newNonce(); | ||
const messageUint8 = decodeUTF8(JSON.stringify(json)); | ||
|
||
const encrypted = box.after(messageUint8, nonce, this._sharedKey); | ||
|
||
const fullMessage = new Uint8Array(nonce.length + encrypted.length); | ||
fullMessage.set(nonce); | ||
fullMessage.set(encrypted, nonce.length); | ||
|
||
const base64FullMessage = encodeBase64(fullMessage); | ||
return base64FullMessage; | ||
} catch (e) { | ||
console.log(e); | ||
return JSON.stringify({msg: "Data encryption error"}); | ||
} | ||
}; | ||
|
||
constructor() { | ||
this._keyPair = box.keyPair(); | ||
}; | ||
|
||
public get publicKey() { | ||
return this._keyPair?.publicKey; | ||
}; | ||
|
||
public setAdminPanelPublicKey(data: number[]) { | ||
if (this._keyPair) { | ||
this._adminPanelPublicKey = new Uint8Array(data); | ||
this._sharedKey = box.before(this._adminPanelPublicKey, this._keyPair.secretKey); | ||
console.log('this._sharedKey',this._sharedKey) | ||
} else { | ||
console.warn("No keypair generated!"); | ||
} | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters