cryptorjs
Advanced tools
Comparing version 1.0.5 to 1.1.0
56
index.js
/** | ||
* Created by Fabio on 21/05/2017. | ||
*/ | ||
'use strict'; | ||
const crypto = require('crypto'); | ||
/** | ||
* Cryptor class | ||
*/ | ||
class Cryptor { | ||
/** | ||
* Cryptor constructor | ||
* @param key | ||
* @param algorithm | ||
*/ | ||
constructor(key, algorithm = 'aes-256-ctr'){ | ||
if(typeof key === 'undefined') | ||
throw new Error('required key'); | ||
if(key === '') | ||
throw new Error('key cannot be empty'); | ||
this.algorithm = algorithm; | ||
this.key = key; | ||
} | ||
/** | ||
* Encode string | ||
* @param str | ||
* @return {string} | ||
*/ | ||
encode(str) { | ||
let cipher = crypto.createCipher(this.algorithm, this.key); | ||
return cipher.update(str, 'utf8', 'hex') + cipher.final('hex'); | ||
} | ||
/** | ||
* Decode string | ||
* @param str | ||
* @return {string} | ||
*/ | ||
decode(str) { | ||
let decipher = crypto.createDecipher(this.algorithm, this.key); | ||
return decipher.update(str, 'hex', 'utf8') + decipher.final('utf8'); | ||
} | ||
/** | ||
* Get available ciphers | ||
* @return {array} | ||
*/ | ||
static getCiphers(){ | ||
return crypto.getCiphers(); | ||
} | ||
} | ||
module.exports = Cryptor; | ||
module.exports = require('./src/cryptor'); |
{ | ||
"name": "cryptorjs", | ||
"version": "1.0.5", | ||
"description": "Encrypt and decrypt string using a key", | ||
"version": "1.1.0", | ||
"description": "Encrypt and decrypt string, number and object using a key", | ||
"main": "index.js", | ||
@@ -25,3 +25,5 @@ "directories": { | ||
"algorithm", | ||
"secret" | ||
"secret", | ||
"json", | ||
"object" | ||
], | ||
@@ -28,0 +30,0 @@ "author": "Fabio Ricali", |
# Cryptorjs | ||
Simple library for encryption and decryption of string using a key | ||
Simple library for encryption and decryption of string, number and object using a key | ||
@@ -25,3 +25,15 @@ [![Build Status](https://travis-ci.org/fabioricali/Cryptor.svg?branch=master)](https://travis-ci.org/fabioricali/Cryptor) [![Coverage Status](https://coveralls.io/repos/github/fabioricali/Cryptor/badge.svg)](https://coveralls.io/github/fabioricali/Cryptor) | ||
``` | ||
### Object encryption | ||
```javascript | ||
var cryptorjs = require('cryptorjs'); | ||
var myCryptor = new cryptorjs('yourSecretKey'); | ||
var encoded = myCryptor.encode({ a: 1, b: 2 }); | ||
// => '2183c42066819ed9184f1df116' | ||
var decoded = myCryptor.decode('2183c42066819ed9184f1df116'); | ||
// => { a: 1, b: 2 } | ||
``` | ||
### With a cipher | ||
@@ -28,0 +40,0 @@ For example using "blowfish" cipher |
@@ -6,2 +6,3 @@ /** | ||
const cryptor = require('../index'); | ||
const helper = require('../src/helper'); | ||
@@ -21,2 +22,47 @@ describe('encode and decode', function () { | ||
it('should be equal using a number', function () { | ||
let origin = 10000; | ||
let myCryptor = new cryptor('yourSecretKey'); | ||
let encoded = myCryptor.encode(origin); | ||
let decoded = myCryptor.decode(encoded); | ||
console.log(origin, encoded, decoded); | ||
assert.equal(origin, decoded); | ||
}); | ||
it('should be equal using a object', function () { | ||
let origin = {a:1,b:2}; | ||
let myCryptor = new cryptor('yourSecretKey'); | ||
let encoded = myCryptor.encode(origin); | ||
let decoded = myCryptor.decode(encoded); | ||
console.log(origin, encoded, decoded); | ||
console.log(origin, typeof origin); | ||
console.log(decoded, typeof decoded); | ||
assert.deepEqual(origin, decoded); | ||
}); | ||
it('origin is empty', function () { | ||
let origin = ''; | ||
let myCryptor = new cryptor('yourSecretKey'); | ||
let encoded = myCryptor.encode(origin); | ||
let decoded = myCryptor.decode(encoded); | ||
console.log(origin, encoded, decoded); | ||
assert.equal(origin, decoded); | ||
}); | ||
it('should be error if origin missing', function (done) { | ||
try { | ||
let myCryptor = new cryptor('yourSecretKey'); | ||
myCryptor.encode(); | ||
myCryptor.decode(); | ||
}catch (error) { | ||
done(); | ||
} | ||
}); | ||
it('should be error if key missing', function (done) { | ||
@@ -57,2 +103,53 @@ try{ | ||
}) | ||
}); | ||
describe('helper function', function () { | ||
describe('normalizeInput', function () { | ||
it('should be return equal string', function () { | ||
let origin = 'hello'; | ||
let result = helper.normalizeInput(origin); | ||
assert.equal(result, origin); | ||
}); | ||
it('should be return equal json string', function () { | ||
let origin = {a:1}; | ||
let result = helper.normalizeInput(origin); | ||
assert.equal(result, JSON.stringify(origin)); | ||
}); | ||
it('should be return equal number', function () { | ||
let origin = 100; | ||
let result = helper.normalizeInput(origin); | ||
assert.equal(result, origin); | ||
assert.strictEqual(Number(result), origin); | ||
}); | ||
it('should be return error if null', function (done) { | ||
try { | ||
helper.normalizeInput(null); | ||
} catch (e) { | ||
done(); | ||
} | ||
}); | ||
it('should be return error if undefined', function (done) { | ||
try { | ||
helper.normalizeInput(); | ||
} catch (e) { | ||
done(); | ||
} | ||
}); | ||
}); | ||
describe('normalizeOutput', function () { | ||
it('should be return equal string', function () { | ||
let origin = 'test'; | ||
let result = helper.normalizeOutput(origin); | ||
assert.equal(result, origin); | ||
}); | ||
it('should be return equal object', function () { | ||
let origin = '{"a":1}'; | ||
let result = helper.normalizeOutput(origin); | ||
assert.deepEqual(result, JSON.parse(origin)); | ||
}); | ||
}); | ||
}); |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
10218
9
215
73