-
Notifications
You must be signed in to change notification settings - Fork 302
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(get): verify sha256 checksum (#1201)
- Loading branch information
1 parent
282ea7a
commit 810944d
Showing
4 changed files
with
71 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import crypto from 'node:crypto'; | ||
import fs from 'node:fs'; | ||
import path from 'node:path'; | ||
|
||
import request from './request.js'; | ||
|
||
import util from '../util.js'; | ||
|
||
/** | ||
* Verify the SHA256 checksum of downloaded artifacts. | ||
* | ||
* @param {string} shaUrl - URL to get the shasum text file from. | ||
* @param {string} shaOut - File path to shasum text file. | ||
* @param {string} cacheDir - File path to cache directory. | ||
* @throws {Error} | ||
* @returns {Promise<boolean>} | ||
*/ | ||
export default async function verify(shaUrl, shaOut, cacheDir) { | ||
const shaOutExists = await util.fileExists(shaOut); | ||
|
||
if (shaOutExists === false) { | ||
/* Create directory if does not exist. */ | ||
await fs.promises.mkdir(path.dirname(shaOut), { recursive: true }); | ||
|
||
/* Download SHASUM text file. */ | ||
await request(shaUrl, shaOut); | ||
} | ||
|
||
/* Read SHASUM text file */ | ||
const shasum = await fs.promises.readFile(shaOut, { encoding: 'utf-8' }); | ||
const shasums = shasum.trim().split('\n'); | ||
for await (const line of shasums) { | ||
const [storedSha, filePath] = line.split(' '); | ||
const relativeFilePath = path.resolve(cacheDir, filePath); | ||
const relativefilePathExists = await util.fileExists(relativeFilePath); | ||
if (relativefilePathExists) { | ||
const fileBuffer = await fs.promises.readFile(relativeFilePath); | ||
const hash = crypto.createHash('sha256'); | ||
hash.update(fileBuffer); | ||
const generatedSha = hash.digest('hex'); | ||
if (storedSha !== generatedSha) { | ||
throw new Error('SHA256 checksums do not match.'); | ||
} | ||
} | ||
} | ||
|
||
return true; | ||
} |
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,15 @@ | ||
import { describe, expect, it } from "vitest"; | ||
|
||
import verify from "./verify.js"; | ||
|
||
describe("get/verify", function () { | ||
|
||
it("verify shasums", async function () { | ||
const status = await verify( | ||
'https://dl.nwjs.io/v0.90.0/SHASUMS256.txt', | ||
'./node_modules/nw/shasum/0.90.0.txt', | ||
'./node_modules/nw' | ||
); | ||
expect(status).toBe(true); | ||
}, Infinity); | ||
}); |