Advertisement
alexdmin

js

Sep 6th, 2021
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const fs = require('fs')
  2. const path = require('path')
  3.  
  4. const  { SHA3 } = require("sha3")
  5.  
  6. const ignore = ["node_modules"]
  7.  
  8. const rstream = f => new Promise( (resolve, reject) => {
  9.     const readStream = fs.createReadStream(f, {highWaterMark: 1024*1024 });
  10.    
  11.     const data = []
  12.     readStream.on('data', (chunk) => {
  13.         data.push(chunk)
  14.     })
  15.  
  16.     readStream.on('end', () => {
  17.         resolve(data)
  18.     })
  19.    
  20.     readStream.on('error', (err) => {
  21.         reject(err)
  22.     })
  23. })
  24.  
  25. const getHash = (entry, data) => {
  26.     const hash = new SHA3(256);
  27.     hash.update(Buffer.concat(data))
  28.     console.log(entry, hash.digest('hex'))
  29. }
  30.  
  31. const procEntry = async entry =>{
  32.     let statEntry = fs.statSync(entry)
  33.     if(statEntry.isFile()){
  34.         try{
  35.             let data = await rstream(entry)
  36.             getHash(entry, data)
  37.         }
  38.         catch(e){
  39.             console.log(e)
  40.         }
  41.        
  42.     }
  43.     else if (statEntry.isDirectory()){
  44.         console.log(entry)
  45.         for(let e of fs.readdirSync(entry)) {
  46.             if(!ignore.includes(e))
  47.                 procEntry(path.join(entry, "\\", e))
  48.         }
  49.     }
  50. }
  51.  
  52. try{
  53.     for(let entry of fs.readdirSync(".")) if(!ignore.includes(entry)) procEntry(entry)
  54. }
  55. catch(e){
  56.     console.error(e.message)
  57. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement