-
-
Notifications
You must be signed in to change notification settings - Fork 56
/
browser.js
91 lines (74 loc) · 2.03 KB
/
browser.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
'use strict'
var sign = require('./sign')
var verify = require('./verify')
var stream = require('stream')
var inherits = require('inherits')
var _algos = require('./algos')
var createHash = require('create-hash')
var algos = {}
Object.keys(_algos).forEach(function (key) {
algos[key] = algos[key.toLowerCase()] = _algos[key]
})
exports.createSign = exports.Sign = createSign
function createSign (algorithm) {
return new Sign(algorithm)
}
exports.createVerify = exports.Verify = createVerify
function createVerify (algorithm) {
return new Verify(algorithm)
}
inherits(Sign, stream.Writable)
function Sign (algorithm) {
stream.Writable.call(this)
var data = algos[algorithm]
if (!data)
throw new Error('Unknown message digest')
this._hashType = data.hash
this._hash = createHash(data.hash)
this._tag = data.id
}
Sign.prototype._write = function _write (data, _, done) {
this._hash.update(data)
done()
}
Sign.prototype.update = function update (data, enc) {
if (typeof data === 'string')
data = new Buffer(data, enc)
this._hash.update(data)
return this
}
Sign.prototype.sign = function signMethod (key, enc) {
this.end()
var hash = this._hash.digest()
var sig = sign(Buffer.concat([this._tag, hash]), key, this._hashType)
if (enc) {
sig = sig.toString(enc)
}
return sig
}
inherits(Verify, stream.Writable)
function Verify (algorithm) {
stream.Writable.call(this)
var data = algos[algorithm]
if (!data)
throw new Error('Unknown message digest')
this._hash = createHash(data.hash)
this._tag = data.id
}
Verify.prototype._write = function _write (data, _, done) {
this._hash.update(data)
done()
}
Verify.prototype.update = function update (data, enc) {
if (typeof data === 'string')
data = new Buffer(data, enc)
this._hash.update(data)
return this
}
Verify.prototype.verify = function verifyMethod (key, sig, enc) {
this.end()
var hash = this._hash.digest()
if (typeof sig === 'string')
sig = new Buffer(sig, enc)
return verify(sig, Buffer.concat([this._tag, hash]), key)
}