Skip to content

Instantly share code, notes, and snippets.

@alickmail
Created October 1, 2019 16:11
Show Gist options
  • Save alickmail/f1ded3c2e912cf107dfd57ecc84a7890 to your computer and use it in GitHub Desktop.
Save alickmail/f1ded3c2e912cf107dfd57ecc84a7890 to your computer and use it in GitHub Desktop.
// 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;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment