md5sum.js 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. const crypto = require('crypto');
  2. const fs = require('fs');
  3. function md5File(path) {
  4. return new Promise((ok, ko) => {
  5. const readStream = fs.createReadStream(path);
  6. let hash = crypto.createHash('md5');
  7. readStream.on('data', chunk => hash.update(chunk.toString()));
  8. readStream.once('end', () => {
  9. ok(hash.digest('hex'));
  10. });
  11. readStream.once('error', () => ok(""));
  12. });
  13. }
  14. function md5String(input) {
  15. return crypto.createHash('md5').update(input).digest('hex');
  16. }
  17. function md5Stats(path) {
  18. return new Promise((ok, ko) => {
  19. fs.stat(path, (err, st) => {
  20. if (err)
  21. ok("");
  22. else
  23. ok(md5String(JSON.stringify({
  24. path: path,
  25. size: st.size,
  26. atimeMs: st.atimeMs,
  27. mtimeMs: st.mtimeMs,
  28. ctimeMs: st.ctimeMs
  29. })));
  30. });
  31. });
  32. }
  33. module.exports = {
  34. file: md5File,
  35. string: md5String,
  36. stats: md5Stats
  37. }