2025-06-19 22:44:47 +00:00
|
|
|
import { tap, expect } from '@git.zone/tstest/tapbundle';
|
2022-06-26 11:34:09 +02:00
|
|
|
import * as fs from 'fs';
|
2016-08-16 03:37:40 +02:00
|
|
|
|
2022-06-26 11:34:09 +02:00
|
|
|
import * as smarthash from '../ts/index.js';
|
2016-08-16 03:37:40 +02:00
|
|
|
|
2018-09-08 01:04:47 +02:00
|
|
|
tap.test('sha256FromStringSync should convert a String to sha256 hash synchronously', async () => {
|
2019-07-04 16:56:37 +02:00
|
|
|
const testHash = smarthash.sha256FromStringSync('test');
|
|
|
|
const testHash2 = smarthash.sha256FromStringSync('testString');
|
|
|
|
const testHash3 = smarthash.sha256FromStringSync('test');
|
2022-06-26 11:34:09 +02:00
|
|
|
expect(testHash).toEqual(testHash3);
|
|
|
|
expect(testHash).not.toEqual('test');
|
2018-09-08 01:04:47 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
tap.test('sha256fromStringSync should convert a String to sha256 hash synchronously', async () => {
|
2019-07-04 16:56:37 +02:00
|
|
|
const resultString = await smarthash.sha256FromString('test');
|
|
|
|
const compareString = smarthash.sha256FromStringSync('test');
|
2022-06-26 11:34:09 +02:00
|
|
|
expect(resultString).toEqual(compareString);
|
2018-09-08 01:04:47 +02:00
|
|
|
});
|
|
|
|
|
2020-10-05 15:07:28 +00:00
|
|
|
tap.test('sha256fromStream should convert a Stream to sha256', async (tools) => {
|
2019-07-04 16:56:37 +02:00
|
|
|
const readStream = fs.createReadStream('./test/testImageForHash.jpg');
|
|
|
|
const resultString: string = await smarthash.sha256FromStream(readStream);
|
2022-06-26 11:34:09 +02:00
|
|
|
expect(resultString).toEqual('45b80413ed93acb495691186ce61850449439f9183352b9bff96d5533fa1046c');
|
2018-09-08 01:04:47 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
tap.test('sha256fromFile should convert a Stream to sha256', async () => {
|
2019-07-04 16:56:37 +02:00
|
|
|
const resultString = await smarthash.sha256FromFile('./test/testImageForHash.jpg');
|
2022-06-26 11:34:09 +02:00
|
|
|
expect(resultString).toEqual('45b80413ed93acb495691186ce61850449439f9183352b9bff96d5533fa1046c');
|
2018-09-08 01:04:47 +02:00
|
|
|
});
|
|
|
|
|
2019-07-04 16:56:37 +02:00
|
|
|
tap.test('should produce reproducible hash from Object', async () => {
|
|
|
|
const hash1 = await smarthash.sha265FromObject({
|
2019-11-21 14:20:57 +00:00
|
|
|
hithere: 1,
|
2020-10-05 15:07:28 +00:00
|
|
|
wow: 'two',
|
2019-07-04 16:56:37 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
const hash2 = await smarthash.sha265FromObject({
|
2019-11-21 14:20:57 +00:00
|
|
|
wow: 'two',
|
2020-10-05 15:07:28 +00:00
|
|
|
hithere: 1,
|
2019-07-04 16:56:37 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
const hash3 = await smarthash.sha265FromObject({
|
2019-11-21 14:20:57 +00:00
|
|
|
wow: 'twoe',
|
2020-10-05 15:07:28 +00:00
|
|
|
hithere: 1,
|
2019-07-04 16:56:37 +02:00
|
|
|
});
|
2022-06-26 11:34:09 +02:00
|
|
|
expect(hash1).toEqual(hash2);
|
|
|
|
expect(hash1).not.toEqual(hash3);
|
2019-07-04 16:56:37 +02:00
|
|
|
});
|
|
|
|
|
2019-11-21 14:17:48 +00:00
|
|
|
tap.test('should create md5hash from string', async () => {
|
|
|
|
const md5Hash = await smarthash.md5FromString('hellothere');
|
2022-06-26 11:34:09 +02:00
|
|
|
expect(md5Hash).toEqual('c6f7c372641dd25e0fddf0215375561f');
|
2019-11-21 14:17:48 +00:00
|
|
|
});
|
|
|
|
|
2025-06-19 22:44:47 +00:00
|
|
|
export default tap.start();
|