Skip to content

Commit

Permalink
feat(hashes.ts): Add SHA3
Browse files Browse the repository at this point in the history
  • Loading branch information
jontey committed Apr 15, 2019
1 parent e0ce2cb commit 8fd6339
Show file tree
Hide file tree
Showing 7 changed files with 161 additions and 42 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
"license": "MIT",
"dependencies": {
"crypto-js": "^3.1.9-1",
"js-sha3": "0.8.0",
"js-sha3": "^0.8.0",
"lodash": "^4.17.10",
"nem-sdk": "^1.6.7",
"nem2-sdk": "^0.11.3",
Expand Down
2 changes: 2 additions & 0 deletions src/hash/hash.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@ export * from './keccak-512';
export * from './md5';
export * from './sha1';
export * from './sha256';
export * from './sha3-256';
export * from './sha3-512';
53 changes: 53 additions & 0 deletions src/hash/sha3-256.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import { sha3_256 } from 'js-sha3';
import * as nemSDK from 'nem-sdk';
import { Account, NetworkType } from 'nem2-sdk';
import { HashFunction } from './HashFunction';

const nem = nemSDK.default;
/**
* @description - SHA3_256 hash function class
* @export
* @class SHA3_256
* @extends {HashFunction}
*/
// tslint:disable-next-line:class-name
export class SHA3_256 extends HashFunction {
/**
* Creates an instance of SHA3_256.
* @memberof SHA3_256
*/
constructor() {
super('10', '90');
}

/**
* @description - creates a signed hash for private apostille
* @param {string} data - raw data
* @param {string} signerPrivateKey - signer private key
* @returns - a signed hash with a magical byte
* @memberof SHA3_256
*/
public signedHashing(data: string, signerPrivateKey: string, networkType: NetworkType): string {
if (networkType === NetworkType.MAIN_NET || networkType === NetworkType.TEST_NET) {
const keyPair = nem.crypto.keyPair.create(signerPrivateKey);
const CHEKSUM = 'fe4e5459' + this.signed;
return CHEKSUM + keyPair.sign(sha3_256(data)).toString();
} else {
// sha-3 signing
const signer = Account.createFromPrivateKey(signerPrivateKey, networkType);
const CHEKSUM = 'fe4e5459' + this.signed;
return CHEKSUM + signer.signData(sha3_256(data));
}
}

/**
* @description - creates a hash of the digital file for public apostille
* @param {string} data - digital file raw data
* @returns - a hash with a magical byte
* @memberof SHA3_256
*/
public nonSignedHashing(data: string): string {
const CHEKSUM = 'fe4e5459' + this.nonSigned;
return CHEKSUM + sha3_256(data);
}
}
53 changes: 53 additions & 0 deletions src/hash/sha3-512.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import { sha3_512 } from 'js-sha3';
import * as nemSDK from 'nem-sdk';
import { Account, NetworkType } from 'nem2-sdk';
import { HashFunction } from './HashFunction';

const nem = nemSDK.default;
/**
* @description - SHA3_512 hash function class
* @export
* @class SHA3_512
* @extends {HashFunction}
*/
// tslint:disable-next-line:class-name
export class SHA3_512 extends HashFunction {
/**
* Creates an instance of SHA3_512.
* @memberof SHA3_512
*/
constructor() {
super('11', '91');
}

/**
* @description - creates a signed hash for private apostille
* @param {string} data - raw data
* @param {string} signerPrivateKey - signer private key
* @returns - a signed hash with a magical byte
* @memberof SHA3_512
*/
public signedHashing(data: string, signerPrivateKey: string, networkType: NetworkType) {
if (networkType === NetworkType.MAIN_NET || networkType === NetworkType.TEST_NET) {
const keyPair = nem.crypto.keyPair.create(signerPrivateKey);
const CHEKSUM = 'fe4e5459' + this.signed;
return CHEKSUM + keyPair.sign(sha3_512(data)).toString();
} else {
// sha-3 signing
const signer = Account.createFromPrivateKey(signerPrivateKey, networkType);
const CHEKSUM = 'fe4e5459' + this.signed;
return CHEKSUM + signer.signData(sha3_512(data));
}
}

/**
* @description - creates a hash of the digital file for public apostille
* @param {string} data - digital file raw data
* @returns - a hash with a magical byte
* @memberof SHA3_512
*/
public nonSignedHashing(data: string) {
const CHEKSUM = 'fe4e5459' + this.nonSigned;
return CHEKSUM + sha3_512(data);
}
}
50 changes: 15 additions & 35 deletions src/types/Errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,45 +5,25 @@
* @enum {number}
*/
export enum Errors {

MISSING_ENDPOINT_ARGUMENT,

MIJIN_ENDPOINT_NEEDED,

MISSING_IS_COMPLETE_ARGUMENT,

NETWORK_TYPE_MISMATCHED,

AGGREGATE_BONDED_NEED_MULTISIG_ACCOUNT,
AGGREGATE_COMPLETE_NEED_MULTISIG_ACCOUNT,
APOSTILLE_ALREADY_CREATED,

APOSTILLE_NOT_CREATED,

AGGREGATE_COMPLETE_NEED_MULTISIG_ACCOUNT,

AGGREGATE_BONDED_NEED_MULTISIG_ACCOUNT,

FILE_ALREADY_ANNOUNCED,

TRANSACTION_INFO_NOT_FOUND,

NOT_PUBLIC_APOSTILLE,

NOT_PRIVATE_APOSTILLE,

NOT_APOSTILLE,

CREATION_TRANSACTIONS_NOT_FOUND,

UNABLE_TO_SIGN_AGGREGATE_TRANSACTION,

FILE_ALREADY_ANNOUNCED,
INITIATOR_NOT_COMPLETE,
INITIATOR_TYPE_ACCOUNT_REQUIRE_ACCOUNT,

INITIATOR_TYPE_MULTISIG_REQUIRE_MULTISIG_INITIATOR,

INITIATOR_TYPE_MULTISIG_REQUIRE_AT_LEAST_ONE_COSIGNER,

INITIATOR_NOT_COMPLETE,

INITIATOR_TYPE_MULTISIG_REQUIRE_MULTISIG_INITIATOR,
INITIATOR_UNABLE_TO_SIGN,

MIJIN_ENDPOINT_NEEDED,
MISSING_ENDPOINT_ARGUMENT,
MISSING_IS_COMPLETE_ARGUMENT,
NETWORK_TYPE_MISMATCHED,
NETWORK_TYPE_NOT_SUPPORTED,
NOT_APOSTILLE,
NOT_PRIVATE_APOSTILLE,
NOT_PUBLIC_APOSTILLE,
TRANSACTION_INFO_NOT_FOUND,
UNABLE_TO_SIGN_AGGREGATE_TRANSACTION,
}
33 changes: 32 additions & 1 deletion tests/e2e/PrivateApostille/Hashes.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

import * as nemDefault from 'nem-sdk';
import { NetworkType } from 'nem2-sdk';
import { KECCAK256, KECCAK512, MD5, SHA1, SHA256 } from '../../../src/hash/hash';
import { KECCAK256, KECCAK512, MD5, SHA1, SHA256, SHA3_256, SHA3_512 } from '../../../src/hash/hash';

const nem = nemDefault.default;

Expand Down Expand Up @@ -82,6 +82,37 @@ describe('Generate correct hash using MIJIN_TEST network type', () => {
expect(keccak512.signedHashing(data, signerPrivateKey, NetworkType.MIJIN_TEST)).toEqual(hash);
});

it('generates checksum of signed hash with SHA3_256', () => {
// tslint:disable-next-line:variable-name
const sha3_256 = new SHA3_256();
const checksum = 'fe4e545990';
// tslint:disable-next-line:max-line-length
expect(sha3_256.signedHashing(data, signerPrivateKey, NetworkType.MIJIN_TEST).substring(0, 10)).toEqual(checksum);
});

it('generates signed hash with SHA3_256', () => {
// tslint:disable-next-line:variable-name
const sha3_256 = new SHA3_256();
// tslint:disable-next-line:max-line-length
const hash = 'fe4e5459904B9C121282E15B76D0F005746E364D75398B8446881762F64231C8E3731864596334CF0CFEA7208525F66E64C1B7C690D39681CF5927001FD3C68E63AAA75A0F';
expect(sha3_256.signedHashing(data, signerPrivateKey, NetworkType.MIJIN_TEST)).toEqual(hash);
});

it('generates checksum of signed hash with SHA3_512', () => {
// tslint:disable-next-line:variable-name
const sha3_512 = new SHA3_512();
const checksum = 'fe4e545991';
// tslint:disable-next-line:max-line-length
expect(sha3_512.signedHashing(data, signerPrivateKey, NetworkType.MIJIN_TEST).substring(0, 10)).toEqual(checksum);
});

it('generates signed hash with SHA3_512', () => {
// tslint:disable-next-line:variable-name
const sha3_512 = new SHA3_512();
// tslint:disable-next-line:max-line-length
const hash = 'fe4e54599167D5E936775F973F9B111A4888DFC6F62EEFFBDE872599E5878803F4E51299ACCE2875970F09060CA50E417BCA9AAB860FA6EA18A1C772673C8B71410A38FB06';
expect(sha3_512.signedHashing(data, signerPrivateKey, NetworkType.MIJIN_TEST)).toEqual(hash);
});
});

// TEST_NET ======================================
Expand Down
10 changes: 5 additions & 5 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -5167,16 +5167,16 @@ js-levenshtein@^1.1.3:
resolved "https://registry.yarnpkg.com/js-levenshtein/-/js-levenshtein-1.1.6.tgz#c6cee58eb3550372df8deb85fad5ce66ce01d59d"
integrity sha512-X2BB11YZtrRqY4EnQcLX5Rh373zbK4alC1FW7D7MBhL2gtcC17cTnr6DmfHZeS0s2rTHjUTMMHfG7gO8SSdw+g==

js-sha3@0.8.0:
version "0.8.0"
resolved "https://registry.yarnpkg.com/js-sha3/-/js-sha3-0.8.0.tgz#b9b7a5da73afad7dedd0f8c463954cbde6818840"
integrity sha512-gF1cRrHhIzNfToc802P800N8PpXS+evLLXfsVpowqmAFR9uwbi89WvXg2QspOmXL8QL86J4T1EpFu+yUkwJY3Q==

js-sha3@^0.6.1:
version "0.6.1"
resolved "https://registry.yarnpkg.com/js-sha3/-/js-sha3-0.6.1.tgz#5b89f77a7477679877f58c4a075240934b1f95c0"
integrity sha1-W4n3enR3Z5h39YxKB1JAk0sflcA=

js-sha3@^0.8.0:
version "0.8.0"
resolved "https://registry.yarnpkg.com/js-sha3/-/js-sha3-0.8.0.tgz#b9b7a5da73afad7dedd0f8c463954cbde6818840"
integrity sha512-gF1cRrHhIzNfToc802P800N8PpXS+evLLXfsVpowqmAFR9uwbi89WvXg2QspOmXL8QL86J4T1EpFu+yUkwJY3Q==

"js-tokens@^3.0.0 || ^4.0.0", js-tokens@^4.0.0:
version "4.0.0"
resolved "https://registry.yarnpkg.com/js-tokens/-/js-tokens-4.0.0.tgz#19203fb59991df98e3a287050d4647cdeaf32499"
Expand Down

0 comments on commit 8fd6339

Please sign in to comment.