Skip to content

Commit

Permalink
feat($tests): Added tests
Browse files Browse the repository at this point in the history
  • Loading branch information
slysterous committed Sep 4, 2017
1 parent 00d30b5 commit 31b4cb6
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 15 deletions.
11 changes: 7 additions & 4 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ const crypto =require('crypto');
* @param {number} length - defines the length of the salt that will be generated
* @return {string} - returns the generated salt
*/
LazyCrypto.prototype.generateSalt=function(length){
LazyCrypto.prototype.generateSalt=generateSalt;
function generateSalt(length)
{
return crypto.randomBytes(Math.ceil(length/2))
.toString('hex') /** convert to hexadecimal format */
.slice(0,length); /** return required number of characters */
Expand All @@ -30,7 +32,8 @@ const crypto =require('crypto');
* @param {strng} salt - Required salt to use on hash creation
* @return {object} - Returns an object containing salt and passwordhash
*/
LazyCrypto.prototype.generateSha512Hash=function(password,salt){
LazyCrypto.prototype.generateSha512Hash=generateSha512Hash
function generateSha512Hash(password,salt){
var hash=crypto.createHmac('sha512',salt); /** Hashing algorithm sha512 **/
hash.update(password);
var value= hash.digest('hex');
Expand All @@ -48,7 +51,7 @@ var hash=crypto.createHmac('sha512',salt); /** Hashing algorithm sha512 **/
* @return {object} containing passwordSalt and passwordHash
*/
LazyCrypto.prototype.generateSha512HashAndSalt=function(password,saltlength){
var salt=GenerateSalt(saltlength);
var salt=generateSalt(saltlength);
var passwordData=generateSha512Hash(password,salt);
return{
passwordSalt:passwordData.salt,
Expand Down Expand Up @@ -79,7 +82,7 @@ LazyCrypto.prototype.validateSha512HashAndSalt=function(password,salt,passwordHa
* @param {number} hours - defines tha ammount of hours the specific token will be available for
* @param {length} length - devi
*/
LazyCrypto.prototype.generateEmailVerificationToken=function(hours,length){
LazyCrypto.prototype.generateVerificationToken=function(hours,length){
//create random character token
var chars = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
var token = '';
Expand Down
64 changes: 53 additions & 11 deletions src/index.test.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,62 @@
var expect =require('chai').expect;
var LazyCrypto=require('./index.js');

var lazyCrypto=new LazyCrypto();
describe("lazy-crypto",function(){
it('dummy test', function(){
expect(true).to.be.true;
it('Dummy Test', function(){
expect(lazyCrypto!=null||lazyCrypto!=undefined).to.be.true;
});

});

//LazyCrypto

//generateEmailVerificationToken
var token=lazyCrypto.generateVerificationToken(4,10);
describe("Generate Verification Token Test",function(){
it("Token contains an expiration date",function(){
expect(token.expires instanceof Date).to.be.true;
})
it("Token contains token string",function(){
expect(token.token!=undefined && token.token!=null);
});
});

//validateSha512HashAndSalt
var salt=lazyCrypto.generateSalt(20);
describe("Generate Salt with length 20 Test",function(){
it("Salt was generated",function(){
expect(salt!=undefined&&salt!=null).to.be.true;
})
it("Salt length is valid",function(){
expect(salt.length==20).to.be.true;
})
});
var hash=lazyCrypto.generateSha512Hash('password',salt);
describe("Generate SHA512 Hash with Salt",function(){
it("Hash was generated",function(){
expect(hash!=undefined&&hash!=null).to.be.true;
})
it("Hash and Salt are present on the result",function(){
expect(hash.passwordSalt!=undefined&&hash.passwordHash!=null);
})
it("Hash and salt are valid!",function(){
expect(lazyCrypto.validateSha512HashAndSalt("password",salt,hash));
})
})
describe('Validation of SHA512 Hash with Salt',function(){
it("Hash and salt are valid!",function(){
expect(lazyCrypto.validateSha512HashAndSalt("password",salt,hash));
})
})
var res=lazyCrypto.generateSha512HashAndSalt('password',20);
describe("Generation of SHA 512 Hash along with a Unique Salt",function(){
it('Hash and Salt Generated',function(){
expect(res.passwordSalt!=undefined && res.passwordSalt!=null);
})
it('Salt generated has correct length',function(){
expect(res.passwordSalt.length==20)
})
it("Hash and salt are valid!",function(){
expect(lazyCrypto.validateSha512HashAndSalt("password",res.passwordSalt,res.passwordHash));
})
})

//generateSha512HashAndSalt

//generateSha512Hash

//generateSalt
//passwordSalt:passwordData.salt,
// passwordHash:passwordData.passwordHash

0 comments on commit 31b4cb6

Please sign in to comment.