68 lines
1.5 KiB
JavaScript
68 lines
1.5 KiB
JavaScript
var sha = require('./sha')
|
|
var rng = require('./rng')
|
|
|
|
var algorithms = {
|
|
sha1: {
|
|
hex: sha.hex_sha1,
|
|
binary: sha.b64_sha1,
|
|
ascii: sha.str_sha1
|
|
}
|
|
}
|
|
|
|
function error () {
|
|
var m = [].slice.call(arguments).join(' ')
|
|
throw new Error([
|
|
m,
|
|
'we accept pull requests',
|
|
'http://github.com/dominictarr/crypto-browserify'
|
|
].join('\n'))
|
|
}
|
|
|
|
exports.createHash = function (alg) {
|
|
alg = alg || 'sha1'
|
|
if(!algorithms[alg])
|
|
error('algorithm:', alg, 'is not yet supported')
|
|
var s = ''
|
|
var _alg = algorithms[alg]
|
|
return {
|
|
update: function (data) {
|
|
s += data
|
|
return this
|
|
},
|
|
digest: function (enc) {
|
|
enc = enc || 'binary'
|
|
var fn
|
|
if(!(fn = _alg[enc]))
|
|
error('encoding:', enc , 'is not yet supported for algorithm', alg)
|
|
var r = fn(s)
|
|
s = null //not meant to use the hash after you've called digest.
|
|
return r
|
|
}
|
|
}
|
|
}
|
|
|
|
exports.randomBytes = function(size, callback) {
|
|
if (callback && callback.call) {
|
|
try {
|
|
callback.call(this, undefined, rng(size));
|
|
} catch (err) { callback(err); }
|
|
} else {
|
|
return rng(size);
|
|
}
|
|
}
|
|
|
|
// the least I can do is make error messages for the rest of the node.js/crypto api.
|
|
;['createCredentials'
|
|
, 'createHmac'
|
|
, 'createCypher'
|
|
, 'createCypheriv'
|
|
, 'createDecipher'
|
|
, 'createDecipheriv'
|
|
, 'createSign'
|
|
, 'createVerify'
|
|
, 'createDeffieHellman'
|
|
, 'pbkdf2'].forEach(function (name) {
|
|
exports[name] = function () {
|
|
error('sorry,', name, 'is not implemented yet')
|
|
}
|
|
})
|