Skip to content

Commit

Permalink
Merge pull request #120 from mrstebo/feature/crypto
Browse files Browse the repository at this point in the history
Added the Crypto faker.
  • Loading branch information
mrstebo authored May 10, 2017
2 parents 112d276 + aca8d24 commit 9b76a39
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ Contents
- [Faker.Commerce](doc/commerce.md)
- [Faker.Company](doc/company.md)
- [Faker.Compass](doc/compass.md)
- [Faker.Crypto](doc/crypto.md)
- [Faker.Date](doc/date.md)
- [Faker.Demographic](doc/demographic.md)
- [Faker.DragonBall](doc/dragon_ball.md)
Expand Down
9 changes: 9 additions & 0 deletions doc/crypto.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Faker.Crypto

```js
Faker.Crypto.md5 //=> "6b5ed240042e8a65c55ddb826c3408e6"

Faker.Crypto.sha1 //=> "4e99e31c51eef8b2d290e709f757f92e558a503f"

Faker.Crypto.sha256 //=> "51e4dbb424cd9db1ec5fb989514f2a35652ececef33f21c8dd1fd61bb8e3929d"
```
2 changes: 2 additions & 0 deletions src/faker.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import * as Color from './faker/color';
import * as Commerce from './faker/commerce';
import * as Company from './faker/company';
import * as Compass from './faker/compass';
import * as Crypto from './faker/crypto';
import * as Date from './faker/date';
import * as Demographic from './faker/demographic';
import * as DragonBall from './faker/dragon-ball';
Expand Down Expand Up @@ -69,6 +70,7 @@ module.exports = {
Commerce,
Company,
Compass,
Crypto,
Date,
Demographic,
DragonBall,
Expand Down
33 changes: 33 additions & 0 deletions src/faker/crypto.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { itemFromCollection } from '../utils/random';
import crypto from 'crypto';

// 0-9, a-z
const CHARACTERS = [...Array(10).keys()].concat([...Array(26).keys()].map(i => String.fromCharCode(97+i)));

export function md5() {
const hash = crypto.createHash('md5');
hash.update(characters(255));
return hash.digest('hex');
}

export function sha1() {
const hash = crypto.createHash('sha1');
hash.update(characters(255));
return hash.digest('hex');
}

export function sha256() {
const hash = crypto.createHash('sha256');
hash.update(characters(255));
return hash.digest('hex');
}

function characters(charCount) {
return [...Array(resolveNumber(charCount)).keys()]
.map(_ => itemFromCollection(CHARACTERS))
.join('');
}

function resolveNumber(n) {
return parseInt(n) < 0 ? 0 : parseInt(n);
}
6 changes: 6 additions & 0 deletions test/faker.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,12 @@ describe('#Faker', () => {
});
});

describe('#Crypto', () => {
it('should be an object', () => {
expect(Faker.Crypto).to.be.a('object');
});
});

describe('#Date', () => {
it('should be an object', () => {
expect(Faker.Date).to.be.a('object');
Expand Down
29 changes: 29 additions & 0 deletions test/faker/crypto.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
'use strict';
const expect = require('chai').expect;
const Crypto = require('../../src/faker/crypto');

describe('Crypto', () => {
describe('#md5', () => {
it('should return a md5', () => {
[...Array(100).keys()].forEach(_ => {
expect(Crypto.md5()).to.match(/^[0-9a-f]+$/);
});
});
});

describe('#sha1', () => {
it('should return a sha1', () => {
[...Array(100).keys()].forEach(_ => {
expect(Crypto.sha1()).to.match(/^[0-9a-f]+$/);
});
});
});

describe('#sha256', () => {
it('should return a sha256', () => {
[...Array(100).keys()].forEach(_ => {
expect(Crypto.sha256()).to.match(/^[0-9a-f]+$/);
});
});
});
});

0 comments on commit 9b76a39

Please sign in to comment.