Created
October 1, 2019 16:06
-
-
Save alickmail/0b2b737e6bd9eff2b88b861e667efc30 to your computer and use it in GitHub Desktop.
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
export const encryptionConfig = { | |
JsonWebTokenKey: 'unity-super-long-secret', | |
ChaChaEncryptionKey: 'f2ZWDvhBXFSxKXpCl0wg9aEZnuXfA+B2c+5RU8wWbfQ=', | |
ChaChaSigPublicKey: '6T7iL581iPWn1X/Nm8AD2PNRFDqGyGlRfslNy0SD63M=', | |
ChaChaSigPrivateKey: 'UDPW1Td0BfG4bFINTU85mrwRp9ipd2iqKg/0n8hkFWbpPuIvnzWI9afVf82bwAPY81EUOobIaVF+yU3LRIPrcw==', | |
}; |
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
// encryptionService | |
import * as config from '../config/EncryptionConfig' | |
import * as sodium from 'sodium-native'; | |
import * as md5 from 'md5'; | |
import * as crypto from 'crypto'; | |
export class EncryptionService { | |
public nonce: number = 0; | |
public timeout: number; | |
public lastRequest: number; | |
private sodium: any; | |
private publicKey: any; | |
private privateKey: any; | |
constructor() { | |
// this.nonce = Math.random() * 256 * 256 * 256 | 0; | |
this.nonce = Math.random() * (1 << 30) | 0; | |
} | |
public signMessage(message: Buffer): Buffer { | |
let signedMessage = Buffer.alloc(sodium.crypto_sign_BYTES + message.length); | |
sodium.crypto_sign(signedMessage, message, Buffer.from(config.encryptionConfig.ChaChaSigPrivateKey, 'base64')); | |
return signedMessage; | |
} | |
public signOpenMessage(signedMessage: Buffer) { | |
let message = Buffer.alloc(signedMessage.length - sodium.crypto_sign_BYTES); | |
sodium.crypto_sign_open(message, signedMessage, this.publicKey); | |
return message; | |
} | |
} |
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
import {assert} from "chai"; | |
import {EncryptionService} from "../Services/EncryptionService"; | |
import * as crypto from 'crypto'; | |
describe('encryption', () => { | |
const encryptionService: EncryptionService = new EncryptionService(); | |
// 1000 * 10 Loops: 5.2 seconds | |
it('encryption performance test', () => { | |
const loop = 1000 * 10; // should use 1000 * 10 | |
const message = Buffer.alloc(1000 * 100); | |
console.time('case1'); | |
for (let i = 0; i < loop; i++) { | |
const signedMessage = encryptionService.signMessage(message); | |
const openedMessage = encryptionService.signOpenMessage(signedMessage); | |
} | |
console.timeEnd('case1'); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment