cryptorjs
Advanced tools
Comparing version 1.0.2 to 1.0.3
24
index.js
@@ -17,3 +17,3 @@ /** | ||
*/ | ||
constructor(key, algorithm){ | ||
constructor(key, algorithm = 'aes-256-ctr'){ | ||
@@ -26,3 +26,3 @@ if(typeof key === 'undefined') | ||
this.algorithm = algorithm || 'aes-256-ctr'; | ||
this.algorithm = algorithm; | ||
this.key = key; | ||
@@ -34,9 +34,7 @@ } | ||
* @param str | ||
* @returns {*} | ||
* @return {string} | ||
*/ | ||
encode(str) { | ||
let cipher = crypto.createCipher(this.algorithm, this.key); | ||
let crypted = cipher.update(str, 'utf8', 'hex'); | ||
crypted += cipher.final('hex'); | ||
return crypted; | ||
return cipher.update(str, 'utf8', 'hex') + cipher.final('hex'); | ||
} | ||
@@ -47,12 +45,18 @@ | ||
* @param str | ||
* @returns {*} | ||
* @return {string} | ||
*/ | ||
decode(str) { | ||
let decipher = crypto.createDecipher(this.algorithm, this.key); | ||
let dec = decipher.update(str, 'hex', 'utf8'); | ||
dec += decipher.final('utf8'); | ||
return dec; | ||
return decipher.update(str, 'hex', 'utf8') + decipher.final('utf8'); | ||
} | ||
/** | ||
* Get available ciphers | ||
* @return {array} | ||
*/ | ||
static getCiphers(){ | ||
return crypto.getCiphers(); | ||
} | ||
} | ||
module.exports = Cryptor; |
{ | ||
"name": "cryptorjs", | ||
"version": "1.0.2", | ||
"version": "1.0.3", | ||
"description": "Encrypt and decrypt string using a key", | ||
@@ -24,3 +24,4 @@ "main": "index.js", | ||
"passkey", | ||
"algorithm" | ||
"algorithm", | ||
"secret" | ||
], | ||
@@ -27,0 +28,0 @@ "author": "Fabio Ricali", |
# Cryptorjs [![Build Status](https://travis-ci.org/fabioricali/Cryptor.svg?branch=master)](https://travis-ci.org/fabioricali/Cryptor) | ||
Encrypt and decrypt string using a key | ||
Simple library for encryption and decryption of string using a key | ||
@@ -11,3 +11,3 @@ # Installation | ||
# Example | ||
### Basic | ||
```javascript | ||
@@ -17,2 +17,3 @@ var cryptorjs = require('cryptorjs'); | ||
var myCryptor = new cryptorjs('yourSecretKey'); | ||
var encoded = myCryptor.encode('myExampleString'); | ||
@@ -23,3 +24,33 @@ // => '37d8e07a3dddc2971f3e53b1021f51' | ||
// => 'myExampleString' | ||
``` | ||
### With a cipher | ||
For example using "blowfish" cipher | ||
```javascript | ||
var cryptorjs = require('cryptorjs'); | ||
var myCryptor = new cryptorjs('yourSecretKey', 'blowfish'); | ||
var encoded = myCryptor.encode('myExampleString'); | ||
// => 'd21c35352099eac53a129a414530c162' | ||
var decoded = myCryptor.decode('d21c35352099eac53a129a414530c162'); | ||
// => 'myExampleString' | ||
``` | ||
### Ciphers | ||
You can get the list with a static method | ||
```javascript | ||
var cryptorjs = require('cryptorjs'); | ||
cryptorjs.getCiphers(); | ||
/*=> [ 'aes-128-cbc', | ||
'aes-128-cbc-hmac-sha1', | ||
'aes-128-cbc-hmac-sha256', | ||
'aes-128-ccm', | ||
'aes-128-cfb', | ||
'aes-128-cfb1', | ||
'aes-128-cfb8',...] | ||
*/ | ||
``` |
@@ -36,2 +36,21 @@ /** | ||
it('should be equal with "blowfish" cipher', function () { | ||
let origin = 'myExampleString'; | ||
let myCryptor = new cryptor('yourSecretKey', 'blowfish'); | ||
let encoded = myCryptor.encode(origin); | ||
let decoded = myCryptor.decode(encoded); | ||
console.log(origin, encoded, decoded); | ||
assert.equal(origin, decoded); | ||
}); | ||
}); | ||
describe('get algorithms', function () { | ||
it('should be array', function () { | ||
let algorithms = cryptor.getCiphers(); | ||
console.log(algorithms); | ||
assert.equal(typeof algorithms, 'object'); | ||
}) | ||
}); |
Sorry, the diff of this file is not supported yet
46428
92
53