-
Notifications
You must be signed in to change notification settings - Fork 251
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add file hashes to asar header (#221)
* feat: add file hashes to asar header * feat: add getRawHeader method to public API * chore: fix lint * chore: update docs * refactor: use integrity instead of hash pairs * feat: add block hashes * fix: ensure executables are extracted with executable permission * fix: ensure symlinks are not deeply resolved when packaging * chore: update test files * chore: remove DS_Store * perf: generate block hashes as we parse the stream * docs: update README with new options * revert * chore: update per feedback
- Loading branch information
1 parent
6fb376b
commit 94cb8bd
Showing
17 changed files
with
137 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
const crypto = require('crypto') | ||
const fs = require('fs') | ||
const stream = require('stream') | ||
const { promisify } = require('util') | ||
|
||
const ALGORITHM = 'SHA256' | ||
// 4MB default block size | ||
const BLOCK_SIZE = 4 * 1024 * 1024 | ||
|
||
const pipeline = promisify(stream.pipeline) | ||
|
||
function hashBlock (block) { | ||
return crypto.createHash(ALGORITHM).update(block).digest('hex') | ||
} | ||
|
||
async function getFileIntegrity (path) { | ||
const fileHash = crypto.createHash(ALGORITHM) | ||
|
||
const blocks = [] | ||
let currentBlockSize = 0 | ||
let currentBlock = [] | ||
|
||
await pipeline( | ||
fs.createReadStream(path), | ||
new stream.PassThrough({ | ||
decodeStrings: false, | ||
transform (_chunk, encoding, callback) { | ||
fileHash.update(_chunk) | ||
|
||
function handleChunk (chunk) { | ||
const diffToSlice = Math.min(BLOCK_SIZE - currentBlockSize, chunk.byteLength) | ||
currentBlockSize += diffToSlice | ||
currentBlock.push(chunk.slice(0, diffToSlice)) | ||
if (currentBlockSize === BLOCK_SIZE) { | ||
blocks.push(hashBlock(Buffer.concat(currentBlock))) | ||
currentBlock = [] | ||
currentBlockSize = 0 | ||
} | ||
if (diffToSlice < chunk.byteLength) { | ||
handleChunk(chunk.slice(diffToSlice)) | ||
} | ||
} | ||
handleChunk(_chunk) | ||
callback() | ||
}, | ||
flush (callback) { | ||
blocks.push(hashBlock(Buffer.concat(currentBlock))) | ||
currentBlock = [] | ||
callback() | ||
} | ||
}) | ||
) | ||
|
||
return { | ||
algorithm: ALGORITHM, | ||
hash: fileHash.digest('hex'), | ||
blockSize: BLOCK_SIZE, | ||
blocks: blocks | ||
} | ||
} | ||
|
||
module.exports = getFileIntegrity |
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters