Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

index.js 2.1KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. var createHash = require('create-hash')
  2. var stream = require('stream')
  3. var inherits = require('inherits')
  4. var sign = require('./sign')
  5. var verify = require('./verify')
  6. var algorithms = require('./algorithms.json')
  7. Object.keys(algorithms).forEach(function (key) {
  8. algorithms[key].id = new Buffer(algorithms[key].id, 'hex')
  9. algorithms[key.toLowerCase()] = algorithms[key]
  10. })
  11. function Sign (algorithm) {
  12. stream.Writable.call(this)
  13. var data = algorithms[algorithm]
  14. if (!data) throw new Error('Unknown message digest')
  15. this._hashType = data.hash
  16. this._hash = createHash(data.hash)
  17. this._tag = data.id
  18. this._signType = data.sign
  19. }
  20. inherits(Sign, stream.Writable)
  21. Sign.prototype._write = function _write (data, _, done) {
  22. this._hash.update(data)
  23. done()
  24. }
  25. Sign.prototype.update = function update (data, enc) {
  26. if (typeof data === 'string') data = new Buffer(data, enc)
  27. this._hash.update(data)
  28. return this
  29. }
  30. Sign.prototype.sign = function signMethod (key, enc) {
  31. this.end()
  32. var hash = this._hash.digest()
  33. var sig = sign(hash, key, this._hashType, this._signType, this._tag)
  34. return enc ? sig.toString(enc) : sig
  35. }
  36. function Verify (algorithm) {
  37. stream.Writable.call(this)
  38. var data = algorithms[algorithm]
  39. if (!data) throw new Error('Unknown message digest')
  40. this._hash = createHash(data.hash)
  41. this._tag = data.id
  42. this._signType = data.sign
  43. }
  44. inherits(Verify, stream.Writable)
  45. Verify.prototype._write = function _write (data, _, done) {
  46. this._hash.update(data)
  47. done()
  48. }
  49. Verify.prototype.update = function update (data, enc) {
  50. if (typeof data === 'string') data = new Buffer(data, enc)
  51. this._hash.update(data)
  52. return this
  53. }
  54. Verify.prototype.verify = function verifyMethod (key, sig, enc) {
  55. if (typeof sig === 'string') sig = new Buffer(sig, enc)
  56. this.end()
  57. var hash = this._hash.digest()
  58. return verify(sig, hash, key, this._signType, this._tag)
  59. }
  60. function createSign (algorithm) {
  61. return new Sign(algorithm)
  62. }
  63. function createVerify (algorithm) {
  64. return new Verify(algorithm)
  65. }
  66. module.exports = {
  67. Sign: createSign,
  68. Verify: createVerify,
  69. createSign: createSign,
  70. createVerify: createVerify
  71. }