Skip to content

Commit

Permalink
Merge pull request #47 from logion-network/hotfix/hashes
Browse files Browse the repository at this point in the history
fix: fixes hash of files.
  • Loading branch information
gdethier authored Nov 15, 2021
2 parents 5e8f694 + 530adf9 commit d8993f3
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 4 deletions.
6 changes: 3 additions & 3 deletions src/logion/lib/crypto/hashing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ function hash(algorithm: string, attributes: any[]): string {

export function sha256File(fileName: string): Promise<string> {
var hash = crypto.createHash("sha256");
const stream = fs.createReadStream(fileName, "binary");
const stream = fs.createReadStream(fileName);
const hasherStream = new Stream.Writable();
hasherStream._write = (chunk, _encoding, next) => {
hash.update(chunk);
next();
hash.update(chunk);
next();
}
const promise = new Promise<string>((success, error) => {
stream.on('end', function() {
Expand Down
Binary file added test/unit/lib/crypto/assets.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
7 changes: 6 additions & 1 deletion test/unit/lib/crypto/hashing.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,15 @@ describe('HashingTest', () => {
sha256HashTest("L1IAt8dg2CXiUjCoVZ3wf4uIJWocNgsmhmswXmH0oAU=", ["ABC", 123, true]);
});

it("hashes file", async () => {
it("hashes text file", async () => {
const hash = await sha256File("test/unit/lib/crypto/file.txt");
expect(hash).toBe("0x0ba904eae8773b70c75333db4de2f3ac45a8ad4ddba1b242f0b3cfc199391dd8");
});

it("hashes binary file", async () => {
const hash = await sha256File("test/unit/lib/crypto/assets.png");
expect(hash).toBe("0x68a87ced4573656b101940c90ac3bdc24b651f688c06e71c70d37f43dfc5058c");
});
});

function sha256HashTest(expectedHash: string, attributes: any[]) {
Expand Down

0 comments on commit d8993f3

Please sign in to comment.