Skip to content

Commit

Permalink
adding hooks and relevant tests, bbumping dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
Drazail committed Aug 3, 2022
1 parent 25cf415 commit 41c6fee
Show file tree
Hide file tree
Showing 51 changed files with 1,897 additions and 969 deletions.
27 changes: 27 additions & 0 deletions JSHash/Hooks/useHash.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/* eslint linebreak-style: ["error", "windows"] */
/* eslint-disable no-use-before-define */

import React, { useState, useEffect } from 'react';
import hashString from '../JSHash.js';
import CONSTANTS from '../../Constants.js';

const useHash = (
hashAlgo = CONSTANTS.HashAlgorithms.md5,
initialMessage = 'hello World',
) => {
const [Algo, setAlgo] = useState(hashAlgo);
const [message, setMessage] = useState(initialMessage);
const [hashed, setHashed] = useState();
useEffect(() => {
const hash = () => hashString(message, Algo)
.then((a) => setHashed(a))
.catch((er) => {
console.error(er);
});
hash();
}, [message, Algo]);

return [hashed, setAlgo, setMessage];
};

export default useHash;
29 changes: 29 additions & 0 deletions JSHash/Hooks/useHmac.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/* eslint linebreak-style: ["error", "windows"] */
/* eslint-disable no-use-before-define */

import React, { useState, useEffect } from 'react';
import hmacString from '../JSHmac.js';
import CONSTANTS from '../../Constants.js';

const useHmac = (
hmacAlgo = CONSTANTS.HmacAlgorithms.HmacMD5,
initialMessage = 'hello World',
initialSecret = 'SecretKey',
) => {
const [Algo, setAlgo] = useState(hmacAlgo);
const [message, setMessage] = useState(initialMessage);
const [secret, setSecret] = useState(initialSecret);
const [hmaced, setHmaced] = useState();
useEffect(() => {
const hmac = () => hmacString(message, secret, Algo)
.then((a) => setHmaced(a))
.catch((er) => {
console.error(er);
});
hmac();
}, [message, secret, Algo]);

return [hmaced, setAlgo, setMessage, setSecret];
};

export default useHmac;
41 changes: 41 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,47 @@ JSHmac("message", "SecretKey", CONSTANTS.HmacAlgorithms.HmacSHA256)

check out the [example](https://github.com/Drazail/react-native-hash/blob/f992bdb09b1df5652a3b1590ca6e903a077ad4e6/example/App.js#L88-L90) for more information.

# React Hooks

Following hooks are available:

```javaScript
useHash(
hmacAlgo?: string = "MD5",
initialMessage: ?string = "hello World",
): [
hashed: string,
setMessage: (message: string) => Promise<void>,
setAlgo: (algo: string) => Promise<void>
];
```
```javaScript
useHmac(
hmacAlgo?: string = "HmacMD5",
initialMessage: ?string = "hello World",
initialSecret: ?string = "SecretKey"
): [
hashed: string,
setMessage: (message: string) => Promise<void>,
setAlgo: (algo: string) => Promise<void>,
setSecret: (secret: string) => Promise<void>
];
```
## Usage
```javaScript
const [hashedMessage, setHashAlgo, setHashMessage] = useHash();
const [hmac, setHmacAlgo, setHmacMessage, setHmacSecret] = useHmac();
```
`hashedMessage` and `hmac` will update after a call to one of the setters is resolved.
note that all the setter functions of these two hooks are async and will return a `promise`.
check out the [example] for more information.
***
# Android
Expand Down
67 changes: 0 additions & 67 deletions __tests__/JSHash.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,7 @@ import {
SHA224Hashes,
SHA384Hashes,
KeccakHashes,
HmacMD5s,
HmacSHA1s,
HmacSHA224s,
HmacSHA256s,
HmacSHA384s,
HmacSHA512s,
} from './C.js';
import hmacString from '../JSHash/JSHmac.js';

describe('JSHash hasString Function', () => {
test('MD5', async () => {
Expand Down Expand Up @@ -92,64 +85,4 @@ describe('JSHash hasString Function', () => {
hashString('value', 'badHashAlgo'),
).rejects.toEqual(new Error('badHashAlgo algorithm is not suported'));
});

test('HmacMD5', async () => {
const iterator = TestStrings.entries();
for (const [index, value] of iterator) {
await expect(
hmacString(value, 'SecretKey', CONSTANTS.HmacAlgorithms.HmacMD5),
).resolves.toEqual(HmacMD5s[index]);
}
});

test('HmacSHA1', async () => {
const iterator = TestStrings.entries();
for (const [index, value] of iterator) {
await expect(
hmacString(value, 'SecretKey', CONSTANTS.HmacAlgorithms.HmacSHA1),
).resolves.toEqual(HmacSHA1s[index]);
}
});

test('HmacSHA224', async () => {
const iterator = TestStrings.entries();
for (const [index, value] of iterator) {
await expect(
hmacString(value, 'SecretKey', CONSTANTS.HmacAlgorithms.HmacSHA224),
).resolves.toEqual(HmacSHA224s[index]);
}
});

test('HmacSHA256', async () => {
const iterator = TestStrings.entries();
for (const [index, value] of iterator) {
await expect(
hmacString(value, 'SecretKey', CONSTANTS.HmacAlgorithms.HmacSHA256),
).resolves.toEqual(HmacSHA256s[index]);
}
});

test('HmacSHA384', async () => {
const iterator = TestStrings.entries();
for (const [index, value] of iterator) {
await expect(
hmacString(value, 'SecretKey', CONSTANTS.HmacAlgorithms.HmacSHA384),
).resolves.toEqual(HmacSHA384s[index]);
}
});

test('HmacSHA512', async () => {
const iterator = TestStrings.entries();
for (const [index, value] of iterator) {
await expect(
hmacString(value, 'SecretKey', CONSTANTS.HmacAlgorithms.HmacSHA512),
).resolves.toEqual(HmacSHA512s[index]);
}
});

test('badHmac', async () => {
await expect(
hmacString('value', 'SecretKey', 'badHmacAlgo'),
).rejects.toEqual(new Error('badHmacAlgo algorithm is not suported'));
});
});
78 changes: 78 additions & 0 deletions __tests__/JSHmac.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/* eslint linebreak-style: ["error", "windows"] */
/* eslint-disable no-restricted-syntax */
/* eslint-disable no-await-in-loop */

import CONSTANTS from '../Constants.js';

import {
TestStrings,
HmacMD5s,
HmacSHA1s,
HmacSHA224s,
HmacSHA256s,
HmacSHA384s,
HmacSHA512s,
} from './C.js';
import hmacString from '../JSHash/JSHmac.js';

describe('JSHash hasString Function', () => {
test('HmacMD5', async () => {
const iterator = TestStrings.entries();
for (const [index, value] of iterator) {
await expect(
hmacString(value, 'SecretKey', CONSTANTS.HmacAlgorithms.HmacMD5),
).resolves.toEqual(HmacMD5s[index]);
}
});

test('HmacSHA1', async () => {
const iterator = TestStrings.entries();
for (const [index, value] of iterator) {
await expect(
hmacString(value, 'SecretKey', CONSTANTS.HmacAlgorithms.HmacSHA1),
).resolves.toEqual(HmacSHA1s[index]);
}
});

test('HmacSHA224', async () => {
const iterator = TestStrings.entries();
for (const [index, value] of iterator) {
await expect(
hmacString(value, 'SecretKey', CONSTANTS.HmacAlgorithms.HmacSHA224),
).resolves.toEqual(HmacSHA224s[index]);
}
});

test('HmacSHA256', async () => {
const iterator = TestStrings.entries();
for (const [index, value] of iterator) {
await expect(
hmacString(value, 'SecretKey', CONSTANTS.HmacAlgorithms.HmacSHA256),
).resolves.toEqual(HmacSHA256s[index]);
}
});

test('HmacSHA384', async () => {
const iterator = TestStrings.entries();
for (const [index, value] of iterator) {
await expect(
hmacString(value, 'SecretKey', CONSTANTS.HmacAlgorithms.HmacSHA384),
).resolves.toEqual(HmacSHA384s[index]);
}
});

test('HmacSHA512', async () => {
const iterator = TestStrings.entries();
for (const [index, value] of iterator) {
await expect(
hmacString(value, 'SecretKey', CONSTANTS.HmacAlgorithms.HmacSHA512),
).resolves.toEqual(HmacSHA512s[index]);
}
});

test('badHmac', async () => {
await expect(
hmacString('value', 'SecretKey', 'badHmacAlgo'),
).rejects.toEqual(new Error('badHmacAlgo algorithm is not suported'));
});
});
Loading

0 comments on commit 41c6fee

Please sign in to comment.