Skip to content

Commit

Permalink
initial encrypt/decrypt methods
Browse files Browse the repository at this point in the history
  • Loading branch information
jonathanong committed May 17, 2014
1 parent 1f88974 commit 427f188
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 11 deletions.
58 changes: 47 additions & 11 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,42 @@ Keygrip.prototype = {
encoding: 'base64',
}

// encrypt a message
Keygrip.prototype.encrypt = function Keygrip$_encrypt(data, key, encoding) {
key = key || this.keys[0]

var cipher = crypto.createCipher(this.cipher, key)
cipher.update(data, encoding)
return cipher
.final(this.encoding)
.replace(/\/|\+|=/g, _sign_replace)
}

// decrypt a single message
// returns false on bad decrypts
Keygrip.prototype._decrypt = function Keygrip$__decrypt(data, key, encoding) {
try {
var cipher = crypto.createDecipher(this.cipher, key)
cipher.update(data, this.encoding)
return cipher.final(encoding)
} catch (err) {
if (/bad decrypt/.test(err.message)) return false
throw err
}
}

// decrypt every key
Keygrip.prototype.decrypt = function Keygrip$_decrypt(data, encoding) {
var keys = this.keys
for (var i = 0, l = keys.length; i < l; i++) {
var message = this._decrypt(data, keys[i], encoding)
if (message !== false) return message
}

return false
}

// message signing
Keygrip.prototype.sign = function Keygrip$_sign(data, key) {
// default to the first key
key = key || this.keys[0]
Expand All @@ -58,17 +94,6 @@ Keygrip.prototype.sign = function Keygrip$_sign(data, key) {
.replace(/\/|\+|=/g, _sign_replace)
}

// replace base64 characters with more friendly ones
var _sign_replacements = {
"/": "_",
"+": "-",
"=": ""
}

function _sign_replace(x) {
return _sign_replacements[x]
}

Keygrip.prototype.verify = function Keygrip$_verify(data, digest) {
return this.index(data, digest) > -1
}
Expand All @@ -85,3 +110,14 @@ Keygrip.prototype.index = function Keygrip$_index(data, digest) {
Keygrip.sign = Keygrip.verify = Keygrip.index = function() {
throw new Error("Usage: require('keygrip')(<array-of-keys>)")
}

// replace base64 characters with more friendly ones
var _sign_replacements = {
"/": "_",
"+": "-",
"=": ""
}

function _sign_replace(x) {
return _sign_replacements[x]
}
16 changes: 16 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,22 @@ describe('keygrip([key])', function () {
hash = keys.sign("bieberschnitzel")
assert.ok(/^[\w\-]{27}$/.test(hash))
})

it('should encrypt a message', function () {
hash = keys.encrypt('lol')
assert.equal('lol', keys.decrypt(hash))
})

it('should return false on bad decryptions', function () {
var keys2 = new Keygrip(['lkjasdf'])
assert.equal(false, keys2.decrypt(hash))
})

it('should throw on bad inputs', function () {
assert.throws(function () {
keys.decrypt(hash + 'asdf')
})
})
})

describe('keygrip([keys...])', function () {
Expand Down

0 comments on commit 427f188

Please sign in to comment.