Repositorio del curso CCOM4030 el semestre B91 del proyecto Artesanías con el Instituto de Cultura

index.js 883B

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. const crypto = require('crypto')
  2. const fs = require('fs')
  3. const BUFFER_SIZE = 8192
  4. function md5FileSync (path) {
  5. const fd = fs.openSync(path, 'r')
  6. const hash = crypto.createHash('md5')
  7. const buffer = Buffer.alloc(BUFFER_SIZE)
  8. try {
  9. let bytesRead
  10. do {
  11. bytesRead = fs.readSync(fd, buffer, 0, BUFFER_SIZE)
  12. hash.update(buffer.slice(0, bytesRead))
  13. } while (bytesRead === BUFFER_SIZE)
  14. } finally {
  15. fs.closeSync(fd)
  16. }
  17. return hash.digest('hex')
  18. }
  19. function md5File (path) {
  20. return new Promise((resolve, reject) => {
  21. const output = crypto.createHash('md5')
  22. const input = fs.createReadStream(path)
  23. input.on('error', (err) => {
  24. reject(err)
  25. })
  26. output.once('readable', () => {
  27. resolve(output.read().toString('hex'))
  28. })
  29. input.pipe(output)
  30. })
  31. }
  32. module.exports = md5File
  33. module.exports.sync = md5FileSync