Skip to content

Commit 41c6fee

Browse files
committed
adding hooks and relevant tests, bbumping dependencies
1 parent 25cf415 commit 41c6fee

51 files changed

Lines changed: 1897 additions & 969 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

JSHash/Hooks/useHash.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/* eslint linebreak-style: ["error", "windows"] */
2+
/* eslint-disable no-use-before-define */
3+
4+
import React, { useState, useEffect } from 'react';
5+
import hashString from '../JSHash.js';
6+
import CONSTANTS from '../../Constants.js';
7+
8+
const useHash = (
9+
hashAlgo = CONSTANTS.HashAlgorithms.md5,
10+
initialMessage = 'hello World',
11+
) => {
12+
const [Algo, setAlgo] = useState(hashAlgo);
13+
const [message, setMessage] = useState(initialMessage);
14+
const [hashed, setHashed] = useState();
15+
useEffect(() => {
16+
const hash = () => hashString(message, Algo)
17+
.then((a) => setHashed(a))
18+
.catch((er) => {
19+
console.error(er);
20+
});
21+
hash();
22+
}, [message, Algo]);
23+
24+
return [hashed, setAlgo, setMessage];
25+
};
26+
27+
export default useHash;

JSHash/Hooks/useHmac.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/* eslint linebreak-style: ["error", "windows"] */
2+
/* eslint-disable no-use-before-define */
3+
4+
import React, { useState, useEffect } from 'react';
5+
import hmacString from '../JSHmac.js';
6+
import CONSTANTS from '../../Constants.js';
7+
8+
const useHmac = (
9+
hmacAlgo = CONSTANTS.HmacAlgorithms.HmacMD5,
10+
initialMessage = 'hello World',
11+
initialSecret = 'SecretKey',
12+
) => {
13+
const [Algo, setAlgo] = useState(hmacAlgo);
14+
const [message, setMessage] = useState(initialMessage);
15+
const [secret, setSecret] = useState(initialSecret);
16+
const [hmaced, setHmaced] = useState();
17+
useEffect(() => {
18+
const hmac = () => hmacString(message, secret, Algo)
19+
.then((a) => setHmaced(a))
20+
.catch((er) => {
21+
console.error(er);
22+
});
23+
hmac();
24+
}, [message, secret, Algo]);
25+
26+
return [hmaced, setAlgo, setMessage, setSecret];
27+
};
28+
29+
export default useHmac;

README.md

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,47 @@ JSHmac("message", "SecretKey", CONSTANTS.HmacAlgorithms.HmacSHA256)
9090

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

93+
# React Hooks
94+
95+
Following hooks are available:
96+
97+
```javaScript
98+
useHash(
99+
hmacAlgo?: string = "MD5",
100+
initialMessage: ?string = "hello World",
101+
): [
102+
hashed: string,
103+
setMessage: (message: string) => Promise<void>,
104+
setAlgo: (algo: string) => Promise<void>
105+
];
106+
```
107+
108+
```javaScript
109+
useHmac(
110+
hmacAlgo?: string = "HmacMD5",
111+
initialMessage: ?string = "hello World",
112+
initialSecret: ?string = "SecretKey"
113+
): [
114+
hashed: string,
115+
setMessage: (message: string) => Promise<void>,
116+
setAlgo: (algo: string) => Promise<void>,
117+
setSecret: (secret: string) => Promise<void>
118+
];
119+
```
120+
## Usage
121+
122+
```javaScript
123+
const [hashedMessage, setHashAlgo, setHashMessage] = useHash();
124+
const [hmac, setHmacAlgo, setHmacMessage, setHmacSecret] = useHmac();
125+
```
126+
127+
`hashedMessage` and `hmac` will update after a call to one of the setters is resolved.
128+
129+
note that all the setter functions of these two hooks are async and will return a `promise`.
130+
131+
check out the [example] for more information.
132+
133+
93134
***
94135
95136
# Android

__tests__/JSHash.test.js

Lines changed: 0 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,7 @@ import {
1414
SHA224Hashes,
1515
SHA384Hashes,
1616
KeccakHashes,
17-
HmacMD5s,
18-
HmacSHA1s,
19-
HmacSHA224s,
20-
HmacSHA256s,
21-
HmacSHA384s,
22-
HmacSHA512s,
2317
} from './C.js';
24-
import hmacString from '../JSHash/JSHmac.js';
2518

2619
describe('JSHash hasString Function', () => {
2720
test('MD5', async () => {
@@ -92,64 +85,4 @@ describe('JSHash hasString Function', () => {
9285
hashString('value', 'badHashAlgo'),
9386
).rejects.toEqual(new Error('badHashAlgo algorithm is not suported'));
9487
});
95-
96-
test('HmacMD5', async () => {
97-
const iterator = TestStrings.entries();
98-
for (const [index, value] of iterator) {
99-
await expect(
100-
hmacString(value, 'SecretKey', CONSTANTS.HmacAlgorithms.HmacMD5),
101-
).resolves.toEqual(HmacMD5s[index]);
102-
}
103-
});
104-
105-
test('HmacSHA1', async () => {
106-
const iterator = TestStrings.entries();
107-
for (const [index, value] of iterator) {
108-
await expect(
109-
hmacString(value, 'SecretKey', CONSTANTS.HmacAlgorithms.HmacSHA1),
110-
).resolves.toEqual(HmacSHA1s[index]);
111-
}
112-
});
113-
114-
test('HmacSHA224', async () => {
115-
const iterator = TestStrings.entries();
116-
for (const [index, value] of iterator) {
117-
await expect(
118-
hmacString(value, 'SecretKey', CONSTANTS.HmacAlgorithms.HmacSHA224),
119-
).resolves.toEqual(HmacSHA224s[index]);
120-
}
121-
});
122-
123-
test('HmacSHA256', async () => {
124-
const iterator = TestStrings.entries();
125-
for (const [index, value] of iterator) {
126-
await expect(
127-
hmacString(value, 'SecretKey', CONSTANTS.HmacAlgorithms.HmacSHA256),
128-
).resolves.toEqual(HmacSHA256s[index]);
129-
}
130-
});
131-
132-
test('HmacSHA384', async () => {
133-
const iterator = TestStrings.entries();
134-
for (const [index, value] of iterator) {
135-
await expect(
136-
hmacString(value, 'SecretKey', CONSTANTS.HmacAlgorithms.HmacSHA384),
137-
).resolves.toEqual(HmacSHA384s[index]);
138-
}
139-
});
140-
141-
test('HmacSHA512', async () => {
142-
const iterator = TestStrings.entries();
143-
for (const [index, value] of iterator) {
144-
await expect(
145-
hmacString(value, 'SecretKey', CONSTANTS.HmacAlgorithms.HmacSHA512),
146-
).resolves.toEqual(HmacSHA512s[index]);
147-
}
148-
});
149-
150-
test('badHmac', async () => {
151-
await expect(
152-
hmacString('value', 'SecretKey', 'badHmacAlgo'),
153-
).rejects.toEqual(new Error('badHmacAlgo algorithm is not suported'));
154-
});
15588
});

__tests__/JSHmac.test.js

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
/* eslint linebreak-style: ["error", "windows"] */
2+
/* eslint-disable no-restricted-syntax */
3+
/* eslint-disable no-await-in-loop */
4+
5+
import CONSTANTS from '../Constants.js';
6+
7+
import {
8+
TestStrings,
9+
HmacMD5s,
10+
HmacSHA1s,
11+
HmacSHA224s,
12+
HmacSHA256s,
13+
HmacSHA384s,
14+
HmacSHA512s,
15+
} from './C.js';
16+
import hmacString from '../JSHash/JSHmac.js';
17+
18+
describe('JSHash hasString Function', () => {
19+
test('HmacMD5', async () => {
20+
const iterator = TestStrings.entries();
21+
for (const [index, value] of iterator) {
22+
await expect(
23+
hmacString(value, 'SecretKey', CONSTANTS.HmacAlgorithms.HmacMD5),
24+
).resolves.toEqual(HmacMD5s[index]);
25+
}
26+
});
27+
28+
test('HmacSHA1', async () => {
29+
const iterator = TestStrings.entries();
30+
for (const [index, value] of iterator) {
31+
await expect(
32+
hmacString(value, 'SecretKey', CONSTANTS.HmacAlgorithms.HmacSHA1),
33+
).resolves.toEqual(HmacSHA1s[index]);
34+
}
35+
});
36+
37+
test('HmacSHA224', async () => {
38+
const iterator = TestStrings.entries();
39+
for (const [index, value] of iterator) {
40+
await expect(
41+
hmacString(value, 'SecretKey', CONSTANTS.HmacAlgorithms.HmacSHA224),
42+
).resolves.toEqual(HmacSHA224s[index]);
43+
}
44+
});
45+
46+
test('HmacSHA256', async () => {
47+
const iterator = TestStrings.entries();
48+
for (const [index, value] of iterator) {
49+
await expect(
50+
hmacString(value, 'SecretKey', CONSTANTS.HmacAlgorithms.HmacSHA256),
51+
).resolves.toEqual(HmacSHA256s[index]);
52+
}
53+
});
54+
55+
test('HmacSHA384', async () => {
56+
const iterator = TestStrings.entries();
57+
for (const [index, value] of iterator) {
58+
await expect(
59+
hmacString(value, 'SecretKey', CONSTANTS.HmacAlgorithms.HmacSHA384),
60+
).resolves.toEqual(HmacSHA384s[index]);
61+
}
62+
});
63+
64+
test('HmacSHA512', async () => {
65+
const iterator = TestStrings.entries();
66+
for (const [index, value] of iterator) {
67+
await expect(
68+
hmacString(value, 'SecretKey', CONSTANTS.HmacAlgorithms.HmacSHA512),
69+
).resolves.toEqual(HmacSHA512s[index]);
70+
}
71+
});
72+
73+
test('badHmac', async () => {
74+
await expect(
75+
hmacString('value', 'SecretKey', 'badHmacAlgo'),
76+
).rejects.toEqual(new Error('badHmacAlgo algorithm is not suported'));
77+
});
78+
});

0 commit comments

Comments
 (0)