Skip to content

Commit

Permalink
Add checksum to Player.
Browse files Browse the repository at this point in the history
Closes #478.
  • Loading branch information
flatheadmill committed Feb 9, 2015
1 parent e51d376 commit 9cfba9d
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 4 deletions.
18 changes: 18 additions & 0 deletions checksum.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
var crypto = require('crypto')

module.exports = function (checksum) {
if (typeof checksum == 'function') return checksum
var algorithm
switch (algorithm = checksum || 'sha1') {
case 'none':
// todo: return null
return function () {
return {
update: function () {},
digest: function () { return '0' }
}
}
default:
return function (m) { return crypto.createHash(algorithm) }
}
}
8 changes: 5 additions & 3 deletions player.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ var ok = require('assert').ok
var fs = require('fs')
var path = require('path')
var cadence = require('cadence/redux')
var checksum = require('./checksum')

function Player (directory) {
this.directory = directory
function Player (options) {
this.directory = options.directory
this.checksum = checksum(options.checksum)
}

// todo: outgoing
Expand Down Expand Up @@ -60,7 +62,7 @@ Player.prototype.readEntry = function (sheaf, buffer, isKey) {
}
ok(!count, 'corrupt line: could not find end of line header')
var fields = buffer.toString('utf8', 0, i - 1).split(' ')
var hash = sheaf.checksum(), body, length
var hash = this.checksum(), body, length
hash.update(fields[2])
if (buffer[i - 1] == 0x20) {
body = buffer.slice(i, buffer.length - 1)
Expand Down
2 changes: 1 addition & 1 deletion strata.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ function extend(to, from) {
}

function Strata (options) {
options.player = new Player(options.directory)
options.player = new Player(options)
options.logger = new Logger(options.directory, this.sheaf)
options.logger._sheaf = this.sheaf = new Sheaf(options)
}
Expand Down

0 comments on commit 9cfba9d

Please sign in to comment.