Skip to content

Commit

Permalink
feat: smaller bundle size using crypto-lite in browser
Browse files Browse the repository at this point in the history
  • Loading branch information
marten-de-vries committed Dec 25, 2015
1 parent 0f6d483 commit 7d4cfff
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 7 deletions.
3 changes: 3 additions & 0 deletions lib/hmac-browser.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = function (key, message) {
return new Buffer(require('crypto-lite').crypto.hmac('sha1', key, message), 'hex');
}
3 changes: 3 additions & 0 deletions lib/hmac.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = function (key, message) {
return require('crypto').createHmac('sha1', key).update(message).digest()
}
7 changes: 3 additions & 4 deletions index.js → lib/index.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
module.exports = calculateSessionId

var crypto = require('crypto')

var base64url = require('base64url')
var createHmac = require('./hmac')
var validate = require('aproba')

function calculateSessionId (username, usersalt, serversecret, timestamp) {
validate('SSSN', arguments)

var timestamp16 = timestamp.toString(16).toUpperCase()
var sessionData = username + ':' + timestamp16
var hmac = crypto.createHmac('sha1', serversecret + usersalt).update(sessionData)
var hmac = createHmac(serversecret + usersalt, sessionData)

return base64url(Buffer.concat([
new Buffer(sessionData + ':'),
hmac.digest()
hmac
]))
}
8 changes: 6 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "couchdb-calculate-session-id",
"description": "calculates valid CouchDB session IDs using username, salt, secret & timestamp",
"main": "index.js",
"main": "lib/index.js",
"scripts": {
"coverage": "nyc report --reporter=text-lcov | coveralls",
"pertest": "standard",
Expand Down Expand Up @@ -31,6 +31,10 @@
},
"dependencies": {
"aproba": "^1.0.1",
"base64url": "^1.0.5"
"base64url": "^1.0.5",
"crypto-lite": "^0.1.0"
},
"browser": {
"./lib/hmac.js": "./lib/hmac-browser.js"
}
}
2 changes: 1 addition & 1 deletion test/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
var test = require('tap').test

var calculateSessionId = require('../index')
var calculateSessionId = require('../')

test('valid arguments CouchDB 1.5', function (t) {
var expectedSessionId = 'amFuOjU2Njg4MkI5OkEK3-1SRseo6yNRHfk-mmk6zOxm'
Expand Down

0 comments on commit 7d4cfff

Please sign in to comment.