node 파일의 해시 값 읽기

1216 단어 node
                  
const crypto = require('crypto');
const fs = require('fs');

start();

async function start() {
    const hash256 = await createFileHash256() //  
    const hash256Sync = createFileHash256Sync() //  
    console.log(hash256)
    console.log(hash256Sync)
}

function createFileHash256() {
    //          
    return new Promise((resolve, reject) => {
        const stream = fs.createReadStream('C:/Users/liujt/Documents/AppDesigner/working/ROOT.war');
        const fsHash = crypto.createHash('sha256');

        stream.on('data', function (d) {
            fsHash.update(d);
        });

        stream.on('end', function () {
            const md5 = fsHash.digest('hex');
            resolve(md5);
        });
    })
}


function createFileHash256Sync() {
    const crypto = require('crypto');
    const fs = require('fs');

    //    Buffer
    const buffer = fs.readFileSync('C:/Users/liujt/Documents/AppDesigner/working/ROOT.war');
    const fsHash = crypto.createHash('sha256');

    fsHash.update(buffer);
    const md5 = fsHash.digest('hex');
    return md5
}

좋은 웹페이지 즐겨찾기