sha1.js 531 B

123456789101112131415161718192021
  1. 'use strict';
  2. var crypto = require('crypto');
  3. function sha1(bytes) {
  4. // support modern Buffer API
  5. if (typeof Buffer.from === 'function') {
  6. if (Array.isArray(bytes)) bytes = Buffer.from(bytes);
  7. else if (typeof bytes === 'string') bytes = Buffer.from(bytes, 'utf8');
  8. }
  9. // support pre-v4 Buffer API
  10. else {
  11. if (Array.isArray(bytes)) bytes = new Buffer(bytes);
  12. else if (typeof bytes === 'string') bytes = new Buffer(bytes, 'utf8');
  13. }
  14. return crypto.createHash('sha1').update(bytes).digest();
  15. }
  16. module.exports = sha1;