@xutils/crypto
v1.1.15
Published
used for encryption methods above ES6, supporting Nodejs and browsers
Downloads
73
Maintainers
Readme
@xutils/crypto
Encryption toolkit based on ES6 and above, supporting md5, ripemd160, sha1, sha2, sha3, hmac, aes, des, tripledes, pbkdf2, rabbit, rc4, and rsa.
Installation
npm install @xutils/crypto
Usage
use in md5
import { md5, Md5, createHash } from '@xutils/crypto';
console.log(md5('hello world'));// 5eb63bbbe01eeed093cb22bb8f5acdc3
console.log(md5('hello world', { length: 16 }));// e01eeed093cb22bb
console.log(md5('hello world', { upper: true }));// 5EB63BBBE01EEED093CB22BB8F5ACDC3
console.log(md5('hello world', { upper: true, length: 16 }));// E01EEED093CB22BB
console.log(new Md5().update('hello world').digest());// 5eb63bbbe01eeed093cb22bb8f5acdc3
console.log(new Md5().update('hello world').digest({ length: 16 }));// e01eeed093cb22bb
console.log(new Md5().update('hello world').digest({ upper: true }));// 5EB63BBBE01EEED093CB22BB8F5ACDC3
console.log(new Md5().update('hello world').digest({ upper: true, length: 16 }));// E01EEED093CB22BB
console.log(createHash('md5').update('hello world').digest());// 5eb63bbbe01eeed093cb22bb8f5acdc3
console.log(createHash('md5').update('hello world').digest({ length: 16 }));// e01eeed093cb22bb
console.log(createHash('md5').update('hello world').digest({ upper: true }));// 5EB63BBBE01EEED093CB22BB8F5ACDC3
console.log(createHash('md5').update('hello world').digest({ upper: true, length: 16 }));// E01EEED093CB22BB
use in ripemd160
import { ripeMd160, RipeMd160, createHash } from '@xutils/crypto';
console.log(ripeMd160('hello world'));// 98c615784ccb5fe5936fbc0cbe9dfdb408d92f0f
console.log(ripeMd160('hello world', { upper: true }));// 98C615784CCB5FE5936FBC0CBE9DFDB408D92F0F
console.log(ripeMd160('hello world', { type: 'base64' }));// mMYVeEzLX+WTb7wMvp39tAjZLw8=
console.log(new RipeMd160().update('hello world').digest());// 98c615784ccb5fe5936fbc0cbe9dfdb408d92f0f
console.log(new RipeMd160().update('hello world').digest({ upper: true }));// 98C615784CCB5FE5936FBC0CBE9DFDB408D92F0F
console.log(new RipeMd160().update('hello world').digest({ type: 'base64' }));// mMYVeEzLX+WTb7wMvp39tAjZLw8=
console.log(createHash('ripemd160').update('hello world').digest());// 98c615784ccb5fe5936fbc0cbe9dfdb408d92f0f
console.log(createHash('ripemd160').update('hello world').digest({ upper: true }));// 98C615784CCB5FE5936FBC0CBE9DFDB408D92F0F
console.log(createHash('ripemd160').update('hello world').digest({ type: 'base64' }));// mMYVeEzLX+WTb7wMvp39tAjZLw8=
Refer to ripemd160 for the use of sha1,sha224,sha256,sha384,sha512,sha3-224,sha3-256,sha3-384 and sha3-512.
use in hmacmd5
import { hmacMd5, HmacMd5, createHmac } from '@xutils/crypto';
console.log(hmacMd5('hello world', 'my key')); // 08603c966d17f021cf5d2dcb277f6e04
console.log(new HmacMd5('my key').update('hello world').digest()); // 08603c966d17f021cf5d2dcb277f6e04
console.log(createHmac('md5', 'my key').update('hello world').digest()); // 08603c966d17f021cf5d2dcb277f6e04
Refer to hmacmd5 for the use of hmacripemd160,hmacsha1,hmacsha224,hmacsha256,hmacsha384,hmacsha512,hmacsha3-224,hmacsha3-256,hmacsha3-384 and hmacsha3-512.
use in pbkdf2
import { pbkdf2, Pbkdf2, createHash } from '@xutils/crypto';
console.log(pbkdf2('hello world', { salt: 'my salt', digest: 'sha1' }));// 0d63b940893356a6abac4ecae80e753f
console.log(new Pbkdf2({ salt: 'my salt', digest: 'sha1' }).update('hello world').digest());// 0d63b940893356a6abac4ecae80e753f
console.log(createHash('pbkdf2', { salt: 'my salt', digest: 'sha1' }).update('hello world').digest());// 0d63b940893356a6abac4ecae80e753f
use in rabbit
import { rabbitEncrypt, rabbitDecrypt, Rabbit, createCipheriv, createDecipheriv } from '@xutils/crypto';
// use class Rabbit
const rabbit = new Rabbit('key', '12345678');
console.log(rabbit.decrypt(rabbit.encrypt('hello world')) === 'hello world'); // true
// use cipher function
console.log(createDecipheriv('rabbit', 'key', '12345678').decrypt(createCipheriv('rabbit', 'key', '12345678').encrypt('hello world')) === 'hello world'); // true
// use encrypt function
console.log(rabbitDecrypt(rabbitEncrypt('hello world', { key: 'key', iv: '12345678' }), { key: 'key', iv: '12345678' }) === 'hello world'); // true
Refer to rabbit for the use of rc4,des,tripledes and aes.Only when the type is inconsistent.
use in rsa
import { RSA, rsaEncrypt, rsaDecrypt } from '@xutils/crypto';
const rsa = new RSA();
// Generate key pairs
const keys = RSA.getKeys();
rsa.setKeys(keys);
console.log(rsa.decrypt(rsaEncrypt(keys.publicKey, 'hello world')) === 'hello world'); // true
console.log(rsaDecrypt(keys.privateKey, rsa.encrypt('hello world')) === 'hello world'); // true
use in nodejs
import { ... } from '@xutils/crypto/node';
...