@synotech/utils
Version:
a collection of utilities for internal use
115 lines (95 loc) • 3.43 kB
text/typescript
import assert from 'assert';
import { Cryptography } from '../cryptography';
describe('Crypto()', () => {
let cryptography: Cryptography;
beforeEach(() => {
cryptography = new Cryptography({
keys: [`D5uBt7z18on1Z1I2`, `fqIlGkitP9Ik7xmp`, `tttiKz8PaaExA6gZ`],
encryptKey: `XyAGkH225w5xFojbgusOec5ceNLNP2YE`,
encryptKeySingle: `SyAGkH225w5xFojbgusOec5ceNLNP2YE`,
});
});
it('Encrypt::TWO-WAY: Should return the original message text after encryption', () => {
const plaintext = 'my message text';
const ciphertext = cryptography.encrypt(plaintext);
const decryptOutput: any = cryptography.decrypt(ciphertext);
assert.equal(decryptOutput, plaintext);
expect(decryptOutput).toBe(plaintext);
});
it('Encrypt::ONE-WAY: Should match the same message always', () => {
const password = 'my message text';
const singleCipher = cryptography.encryptSingle(password);
const singleCipherSecondary = cryptography.encryptSingle(password);
assert.equal(singleCipher, singleCipherSecondary);
expect(singleCipher).toBe(singleCipherSecondary);
});
it('Encrypt::Password: Salt and Verify', async () => {
const password = ` `;
const salt = cryptography.salt();
const saltedPassword = cryptography.password(password, salt);
const saltedPasswordSecondary = cryptography.password(password, salt);
assert.equal(saltedPassword, saltedPasswordSecondary);
expect(saltedPassword).toBe(saltedPasswordSecondary);
});
it('Encrypt::JWT: Issue and Verify', async () => {
// @ts-ignore
const jwt = cryptography.jwtIssue(
{ signature: cryptography.signature() },
'1d'
);
assert.ok(cryptography.jwtVerify(jwt));
});
it('Encrypt::RSA: issue private key and create public pair', async () => {
const privateKey = cryptography.privateKey();
const authorityKey = cryptography.publicKey(privateKey.toString());
const isValid = cryptography.publicKeyVerify({
privateKey: privateKey.toString(),
publicKey: authorityKey.toString(),
});
assert.ok(authorityKey);
assert.ok(privateKey);
assert.equal(isValid, true);
});
it('Encrypt::Address: Generate Wallet Address', async () => {
const address = cryptography.address();
assert.ok(address);
});
it('Encrypt::Signature: Issue and Verify', async () => {
const signature = cryptography.signature();
assert.ok(cryptography.signatureVerify(signature));
});
it('Encrypt::Base64: Encode and Decode', async () => {
const raw = 'This is good!';
const encoded = cryptography.base64Encode(raw);
assert.ok(encoded);
const decoded = cryptography.base64Decode(encoded);
assert.ok(decoded);
expect(raw).toBe(decoded);
});
it('should generate a decent password', () => {
const password = cryptography.get('decent_pw');
expect(password.length).toBe(10);
});
it('should generate a strong password', () => {
const password = cryptography.get('strong_pw');
expect(password.length).toBe(15);
});
it('should generate a custom key with specified options', () => {
const options: any = {
length: 12,
useLowerCase: true,
useUpperCase: false,
useNumbers: true,
useSpecial: true,
useHex: false
};
const customKey = cryptography.random(options)
expect(customKey.length).toBe(12)
})
it('should throw an error for unknown strength level', () => {
expect(() => {
// @ts-ignore
cryptography.get('unknown_strength');
}).toThrowError('No such strength');
});
});