Skip to content
This repository has been archived by the owner on Jan 22, 2021. It is now read-only.

Commit

Permalink
Remove unused crypto functions
Browse files Browse the repository at this point in the history
  • Loading branch information
stayradiated committed Feb 21, 2014
1 parent 1e8992b commit 2898293
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 104 deletions.
64 changes: 8 additions & 56 deletions source/core/controllers/crypto.coffee
Expand Up @@ -19,11 +19,10 @@ crypto =
* crypto.hash
*
* Hash some data using bcrypt with a randomly generated salt.
*
* salt:rounds = 10
*
* - data (string)
* > hashed data (string)
* > promise > hashed data (string)
###

hash: (data) ->
Expand All @@ -37,7 +36,7 @@ crypto =
*
* - data (string)
* - hash (string)
* > boolean
* > promise > boolean
###

compare: (data, hash) ->
Expand All @@ -51,62 +50,15 @@ crypto =
* Used to protect random tokens
*
* - data (string) : plaintext
* > string : base64 encoded (NOT URL SAFE)
###


fastHash: (data) ->
nodeCrypto.createHash('sha256')
.update(data, 'utf-8')
.digest('base64')


###
* crypto.fastCompare
*
* Quickly check some data against a 'fastHash'
*
* - data (string) : plaintext
* - hash (string) : hash
* > boolean
* > string : url safe base64 encoded
###

fastCompare: (data, hash) ->
crypto.fastHash(data) is hash


###
* crypto.randomBytes
*
* Generates secure random data.
* Wrap crypto.randomBytes in a promise.
*
* - len (int) : number of bytes to get
* > random data (buffer)
###

randomBytes: Promise.promisify(nodeCrypto.randomBytes, crypto)


###
* crypto.randomToken
*
* Generate a random string of a certain length.
* It generates random bytes and then converts them to url safe base64.
*
* 3 bytes is equal to 4 base64 chars.
* To optimize the amount of random bytes we generate,
* we multiple the length by 3/4 and add 1.
* This generates just enough bytes, and then we trim the output to match
* the original length;
*
* - len (int) : The length of the string
* > random token (string)
###
sha256: (data) ->
buffer = nodeCrypto.createHash('sha256')
.update(data, 'utf-8')
.digest()
base64.encode(buffer)

randomToken: (len) ->
byteLen = Math.floor(len * 0.75) + 1
crypto.randomBytes(byteLen).then (buf) ->
base64.encode(buf)[0 ... len]

module.exports = crypto
56 changes: 8 additions & 48 deletions source/test/core/crypto.coffee
Expand Up @@ -34,57 +34,17 @@ describe 'Crypto', ->
.then -> done()
.done()

describe ':fastHash', ->
describe ':sha256', ->

it 'should quickly hash data', (done) ->
it 'should quickly hash data', ->

crypto.randomToken(64)
.then (string) ->
crypto.fastHash(string)
.then -> done()
.done()
string = 'some_random_string_that_is_rather_long'
expectedHash = 'HYiBIqGYFV9YyIwWYTH1qea2hX9EaZML3K6akqIo6iE'
crypto.sha256(string).should.equal(expectedHash)

it 'should hash strings as utf-8', ->

string = 'aabbccddeeff'
hash = crypto.fastHash(string)
hash.should.equal('wXmVZPLu/K9j3S5cwIVz5jhWIiojLastkaF7Iygw1DA=')

describe ':fastCompare', ->

it 'should quickly compare data', (done) ->

crypto.randomToken(64)
.then (string) ->
hash = crypto.fastHash(string)
crypto.fastCompare(string, hash).should.equal(true)
.then -> done()
.done()

describe ':randomBytes', ->

it 'should generate random bytes', (done) ->

size = 30
string = '0xdeadbeef'
hash = crypto.sha256(string)
hash.should.equal('QUJxC5tMqusAC45d4nG766x_UJqrL15h0e0ZWL_m1YM')

crypto.randomBytes(size)
.then (bytes) ->
bytes.should.have.length(size)
.then -> done()
.done()

describe ':randomToken', ->

it 'should be the correct length', (done) ->

sizes = (i for i in [0 .. 80])

Promise.map sizes, (size) ->
crypto.randomToken(size)

.map (token, size) ->
token.should.have.length(size)
token.should.match(/^[\w-]*$/)

.then -> done()
.done()

0 comments on commit 2898293

Please sign in to comment.