Skip to content

Commit db2241a

Browse files
committed
JSHmac, JSHmac tests, rejection tests, updating readMe
1 parent f85f2cc commit db2241a

71 files changed

Lines changed: 12993 additions & 878 deletions

Some content is hidden

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

Constants.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ const CONSTANTS = {
1919
HmacSHA256: 'HmacSHA256',
2020
HmacSHA384: 'HmacSHA384',
2121
HmacSHA512: 'HmacSHA512',
22+
HmacKeccak: 'HmacKeccak',
2223
PBEwithHmacSHA: 'PBEwithHmacSHA',
2324
PBEwithHmacSHA1: 'PBEwithHmacSHA1',
2425
PBEwithHmacSHA224: 'PBEwithHmacSHA224',

JSHash/JSHash.js

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@ import Hash from './lib/index.js';
66
const {
77
MD5,
88
SHA1,
9-
SHA256,
109
SHA224,
11-
SHA512,
10+
SHA256,
1211
SHA384,
12+
SHA512,
1313
SHA3,
1414
format: { Hex },
1515
} = Hash;
@@ -22,32 +22,32 @@ const {
2222
const hashString = async (string, algorithm) => {
2323
switch (algorithm) {
2424
case 'MD5': {
25-
const md5hash = await MD5(string);
26-
return Hex.stringify(md5hash);
25+
const hash = await MD5(string);
26+
return Hex.stringify(hash);
2727
}
2828
case 'SHA-1': {
29-
const sha1hash = await SHA1(string);
30-
return Hex.stringify(sha1hash);
29+
const hash = await SHA1(string);
30+
return Hex.stringify(hash);
3131
}
3232
case 'SHA-256': {
33-
const sha256hash = await SHA256(string);
34-
return Hex.stringify(sha256hash);
33+
const hash = await SHA256(string);
34+
return Hex.stringify(hash);
3535
}
3636
case 'SHA-224': {
37-
const sha224hash = await SHA224(string);
38-
return Hex.stringify(sha224hash);
37+
const hash = await SHA224(string);
38+
return Hex.stringify(hash);
3939
}
4040
case 'SHA-512': {
41-
const sha512hash = await SHA512(string);
42-
return Hex.stringify(sha512hash);
41+
const hash = await SHA512(string);
42+
return Hex.stringify(hash);
4343
}
4444
case 'SHA-384': {
45-
const sha384hash = await SHA384(string);
46-
return Hex.stringify(sha384hash);
45+
const hash = await SHA384(string);
46+
return Hex.stringify(hash);
4747
}
4848
case 'keccak': {
49-
const keccakhash = await SHA3(string);
50-
return Hex.stringify(keccakhash);
49+
const hash = await SHA3(string);
50+
return Hex.stringify(hash);
5151
}
5252
default:
5353
throw new Error(`${algorithm} algorithm is not suported`);

JSHash/JSHmac.js

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/* eslint linebreak-style: ["error", "windows"] */
2+
/* eslint-disable no-use-before-define */
3+
4+
import Hash from './lib/index.js';
5+
6+
const {
7+
HmacMD5,
8+
HmacSHA1,
9+
HmacSHA224,
10+
HmacSHA256,
11+
HmacSHA384,
12+
HmacSHA512,
13+
format: { Hex },
14+
} = Hash;
15+
16+
/**
17+
*
18+
* @param {string} string
19+
* @param {string} algorithm
20+
*/
21+
const hmacString = async (string, key, algorithm) => {
22+
switch (algorithm) {
23+
case 'HmacMD5': {
24+
const HMac = await HmacMD5(string, key);
25+
return Hex.stringify(HMac);
26+
}
27+
28+
case 'HmacSHA1': {
29+
const HMac = await HmacSHA1(string, key);
30+
return Hex.stringify(HMac);
31+
}
32+
33+
case 'HmacSHA224': {
34+
const HMac = await HmacSHA224(string, key);
35+
return Hex.stringify(HMac);
36+
}
37+
38+
case 'HmacSHA256': {
39+
const HMac = await HmacSHA256(string, key);
40+
return Hex.stringify(HMac);
41+
}
42+
case 'HmacSHA384': {
43+
const HMac = await HmacSHA384(string, key);
44+
return Hex.stringify(HMac);
45+
}
46+
case 'HmacSHA512': {
47+
const HMac = await HmacSHA512(string, key);
48+
return Hex.stringify(HMac);
49+
}
50+
default:
51+
throw new Error(`${algorithm} algorithm is not suported`);
52+
}
53+
};
54+
55+
export default hmacString;

README.md

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,26 +32,45 @@ const hmacAlgorithm = CONSTANTS.HmacAlgorithms.HmacSHA512;
3232
const EventName = CONSTANTS.Events.onBatchReccieved;
3333

3434
```
35-
### Cross Platform API
35+
***
36+
# Cross Platform API
37+
38+
[Native hashing](https://github.com/Drazail/react-native-hash/blob/master/README.md#android) is only implemented on Android, however, until I get around writing native modules for other platforms ( or if some kind soul makes a PR), you can use `JSHash` and `JSHmac`:
39+
40+
***
41+
**NOTE**
42+
43+
if you are using expo, `JSHash` and `JSHmac` should work out of the box, native implementations however, will require you to eject the project.
44+
***
3645

37-
[Native hashing](https://github.com/Drazail/react-native-hash/blob/master/README.md#android) is only implemented on Android, however, until I get around writing native modules for other platforms ( or if some kind soul makes a PR), you can use `JSHash`:
3846

3947
#### Hash Algorithm :
4048

41-
`"md5" | "sha1" | "sha256" | "sha224" | "sha512" | "sha384" | "keccak"`
49+
`"MD2" | "MD5"| "SHA-1"| "SHA-224" | "SHA-256" | "SHA-384" | "SHA-512"| "keccak"`
50+
51+
#### HMac Algorithm :
52+
53+
`"HmacMD5" | "HmacSHA1" | "HmacSHA224" | "HmacSHA256" | "HmacSHA384" | "HmacSHA512"`
4254

4355
#### API:
4456

4557
```
4658
JSHash(message: string, algorithm: string):Promise<string>;
4759
```
60+
```
61+
JSHmac(message: string, secret: string, algorithm: string): Promise<string>;
62+
```
4863

4964
#### Example :
5065

5166
```javascript
52-
import { JSHash, CONSTANTS } from "react-native-hash";
67+
import { JSHash, JSHmac, CONSTANTS } from "react-native-hash";
5368

54-
JSHash("message", CONSTANTS.HashAlgorithms.keccak)
69+
JSHash("message", CONSTANTS.HashAlgorithms.sha256)
70+
.then(hash => console.log(hash))
71+
.catch(e => console.log(e));
72+
73+
JSHmac("message", "SecretKey", CONSTANTS.HmacAlgorithms.HmacSHA256)
5574
.then(hash => console.log(hash))
5675
.catch(e => console.log(e));
5776
```
@@ -60,7 +79,9 @@ JSHash("message", CONSTANTS.HashAlgorithms.keccak)
6079

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

63-
### Android
82+
***
83+
84+
# Android
6485

6586
#### Hash Algorithm :
6687

@@ -172,7 +193,6 @@ check out the [example](https://github.com/Drazail/react-native-hash/blob/6548c1
172193

173194
- SHA-3
174195
- other Keccak lengths
175-
- fully implementing HMAC
176196

177197
### Status
178198

@@ -183,7 +203,9 @@ check out the [example](https://github.com/Drazail/react-native-hash/blob/6548c1
183203
| hash network responses | :x: | :heavy_check_mark: | :x: |
184204
| hash bundle assets | :x: | :x: | :x: |
185205
| hash strings | :heavy_check_mark: | :heavy_check_mark: | :heavy_check_mark: |
186-
| HMAC | :x: | :heavy_check_mark: | :x: |
206+
| HMAC | :heavy_check_mark: | :heavy_check_mark: | :heavy_check_mark: |
207+
208+
187209

188210
- all PRs are welcome
189211

__tests__/C.js

Lines changed: 74 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ export const Md5Hashes = [
2626
'bc2b9e80661f92bd92f7d1c38fda45b4',
2727
];
2828

29-
3029
export const SHA1Hashes = [
3130
'3b13aaa4b9db23c447f5980ddb21edc728a2a1c7',
3231
'12a0c65edc236e265c7e0cd640b0a8f2122622b6',
@@ -53,7 +52,6 @@ export const SHA224Hashes = [
5352
'de694feb9bb07b408ac53eca0f3227e7c781ff389e645e9e6189081f',
5453
];
5554

56-
5755
export const SHA256Hashes = [
5856
'c5b228819cadb84f6942b2d6ba92f9c81216f9b134cb7ede7f5a80ab3a12d7c5',
5957
'2bc90ac81c7b2dcdf2e7ef0b7b8380992f90592fe6d695cd8f766f3fe6685475',
@@ -93,7 +91,6 @@ export const SHA512Hashes = [
9391
'6663cbd90863b625a34c018653304ba5f38b3e2c8ee12b5cf4c0888c59315f45d3da64de7ee2205ebb5909868b877f41cd6172af5ece1ee8fce8266892e3f85c',
9492
];
9593

96-
9794
export const KeccakHashes = [
9895
'605ad18dd4099184b639b639bb3e2f679815346786368eb3b8df4a4334de58e0239fb05cf4c271c1b629cc18886a02b09fc73ae52c839afbabf33fe65a4aa519',
9996
'46ae030006973a2364e96b1fba36781191ff856c1df59fa46c342b8c3695570b47be85f5393d9322dea9f09d7bc00e251a3e05212f8ed73cab026f955cdb9304',
@@ -106,3 +103,77 @@ export const KeccakHashes = [
106103
'3eb3697b783bca8bec67465dee6e61cfdd57f607a0ec74c485b9910fb63574696790627433ef68f6004a7b5a194ffc43ef18549632b6c0c4d044e0a2e864a341',
107104
'b4bd752b678bd4984b525ac96ed926fbaa2a9d947511e2eedbff6014d6bfd252ef869f39d12e134eeea4d1f3d081e16ed0623b5b55c2fac82f4aa78a81cd3058',
108105
];
106+
107+
export const HmacMD5s = [
108+
'748889632936ea35596b7b168b091c46',
109+
'ea46c9997bbb207d708ebd3803a95c95',
110+
'a5b781d647c9c543bde19a8f72e66f78',
111+
'696f256ea4f4abb58cc6de0fdf394987',
112+
'e777e13c8b1b83fd09be00a34859ff0a',
113+
'a84c8d8e5a28bbe5fa2ed082dcc63699',
114+
'c777f86f8769af6879e63fb1e49dcc85',
115+
'6138a50c103ca5530095addb3f98b4af',
116+
'9c838331be63395c09137c19dd791c8b',
117+
'356a60aebbee31e3720c3837ce285ac6',
118+
];
119+
120+
export const HmacSHA1s = [
121+
'a10243addc246736db4789abcf43851678635d88',
122+
'83f6f2aa70502e442b68c81e8507a30520318505',
123+
'9b94353b9ce379a5b2fb00151ba234c4cf5b7b13',
124+
'52bd32b028de2a845e405a0ba54243e9c9dd23a1',
125+
'a83b61487daf89c7f1e22ca4111c2ea17b56e042',
126+
'b403275b16fac1286c358d27dc86e12ebca7cbbd',
127+
'2eb392eadc64a638c0e9f9ebb519025de0b56e64',
128+
'2d793bbbbfaaa7455e4db91921a96f285e25a246',
129+
'709654d5292a2a1dededf18ed82c26297031d704',
130+
'3d393a448b2847c1f4bd14c2ce962ac7883bc974',
131+
];
132+
export const HmacSHA224s = [
133+
'53c806b54a6f27b31e7e7f0d5860837ef08992d4854c4f632113b8f8',
134+
'23d975ecbe4c45f9ecc7826f94494fb5b95d10d2e58ef422dd8a2fe7',
135+
'f0bcb13f422bedde9ac5b446cec301c34f35e2bab2791bb305c1ddd3',
136+
'b0deba2528b94be953e116b4680ed8087c5df86f6e2629530b3e04b5',
137+
'814f38ad8d14b4fa0b4c501f14daeaf52fc4cbf83618aa0d31bcef3f',
138+
'2af010cd545e6786c5f9f4af2f05883a6015c8f496cfeb08aaad35d3',
139+
'cbd7908fd7d4c3d263643d28817a9bf23eab56be774e07806c65d145',
140+
'8e65e43d3dea30fa881a94c6e6fbc815d9568aa74bc42f2de7e8d1ea',
141+
'bbc59772df7810dda39889dbf92db7cc38dae8881ff93a1dd44b0f79',
142+
'6925b5b3cb55eb33229b47b88f625bd99a9c881dda9425cec2a44025',
143+
];
144+
export const HmacSHA256s = [
145+
'4897d258ad80d91b937a512185a00286b6a7cdbb24d5f117266d25915d8352e7',
146+
'2104295906bb6b69722cd6350ebb7de47840da946830c9fdc8bf417be6efdefd',
147+
'374f1b9067bef2a7603528761ab6b122247edda8536b63212b337e8b0ec0ff09',
148+
'81fda63d8c2f61d77383ba3e97751b676036f8c5f1ce50a212e704727b445a5e',
149+
'e29165e344b66b8ec695f4a9bb53490f75e69fe77ec874bcf74ea1979a3190cb',
150+
'21d32a60335d6bf60ae03200c5317c553600420effcfaef68c4ddc6fb0b4196c',
151+
'b8496df72cd941d63b015886513415daf37e4096fca1cea504079b97daed04b4',
152+
'740e41ee32532bdf95d1b3f55a17c0bd1dcf66ef887af993e78e199bf528d897',
153+
'011ec9ee63218632143fd3a699eb1b6c4dce939a709e5adaa248e2af52591f1b',
154+
'f8e19bd799a0de44e755bb042e8c563eddeddcdfb97283e9738e426dd31e6d8f',
155+
];
156+
export const HmacSHA384s = [
157+
'93348d6f3560dffd06447a2bf880fe22e7d32cf79a880611035a98bb3bbf435a3cc42222e8019c89395a87c6018d9a68',
158+
'fd2e7bb5ba0f7083f1efba8efadf5770f0d958d24ce41771e453f3c7c21727c01b3f5049c72e7511f5efe0803f985323',
159+
'c26ea7c941de3f619467cd612e3a0cf4ce067c2b44add8cf12605b41e0c403f72bd56d2cc37997ed2d8287441fd9d501',
160+
'59e2b148078132be1a66299a0142abfafbb6b1b3c74fe7ac74e40e6c9e6cae7ba6275db233505fed799924405a273418',
161+
'0f6d31cc989f894a56a57b59fe5e045f62c01d63066b22f48704f5299af3a69fde8d7d24040565222ba6082ca2fe114b',
162+
'484b867672907031eca07281eda343df2ed7223b540adb27e96d1611c722d562cd69f13382e73e0277e278ae6e636b25',
163+
'afbb8b1789b5ff0bae94c47564ba3c4b1058334c1ceca3f4b5082b0ddc00496e6f72cd07106d5e05da68e8a6f8c99905',
164+
'2296c764ae33f6c66b799b1a9038c3de230ce72e9a11f2eba3d81e29dc31a0c59e765a4985e4479a951f4dd88dbcd280',
165+
'2f01b252b9d8a038bccde5e68d9e3d2703d8893dedc847cade2dff4042ee2a77a1f876631c7e44ba2011a8ad80c5f237',
166+
'cf0ee2ed40091695bb4f1b69daa76527751310c0599bc85f7a423e79aa300c5ec29cb1570d1ba3b8f6c176413009c8f8',
167+
];
168+
export const HmacSHA512s = [
169+
'8dee6c55848c26b97b2e6da2ed6bb62122818689e8827427d553858c98a1bab579dbcebadc2d44a28765cfdff3a136cd568ad4ce0593707c386dc358d7f22409',
170+
'fb0d56555bf6c366cfa4f6cf4f0487032cf32396bea5999498a739eb6c3058c833d5f0f2df00b24863060f8bbd710d02923815787fcc537eb791785b1d43618b',
171+
'97c1bb4b43ba62d387165b43e8f792ba38d7f9437528e80dcd6ef2acc3c0559fad85d499d53de5ebc8e107508a39ae0327ca2554b4f46d2b0d9c135838ef9cfa',
172+
'0d0b1ca2bb4883faab8959039c9b94d53834f96718a7bb99faf5c4e7eee0f8000be320094744912df9be5b7dc939dc5eb86c0cf528265239e3dee0df3841a531',
173+
'cd8dd83afa1ba5d0ffd852e3fef0ab28a7bbc458ea24b644e2a41124a4a0d6adc9a06a31f2d23d284c6be5150db842e4c1adccac78bd3133b16df8cf6b81816a',
174+
'1c0c9124604d50498ce9ff598f722dc6b3fd1b799e7635c271e29bbcc11d4d616d7cc5dd5acb82cb4cfb414a7162c0f2c58b9f1ae94abf103114f8fff4b75c8c',
175+
'1ef19031b6b0b998cec8bf939d3d3b2659af878af6b5373e760e2828a975f1458183a6eab2a80da8688b82e46c731e896261ea73fcc79a9789a44b96edf25de5',
176+
'47860823fb6b496f5cb895f429b3c6dd8bd40c1ad32d164d3926a10e0429f69b8b6af2406847b70aec9bddc23d3a6dbc5fc6db6762def0dcfe115eb3ca00d9a9',
177+
'25bb10507d8d7cbd9c392c2b79209471524032d4c9a0a69d6a990f4ecbabdc32efdae462bc0344dfbb6e7f52a4f26f724413a2ac3a72d23b1adb113d779fd164',
178+
'cce44221fe74defc5bf1f14761c7482969ef2e8a282dd0918d276055eb62b1509cdbdac597f461849f047c6624ef260f6436d8cc2c54575cf46d0fdc8a110721',
179+
];

0 commit comments

Comments
 (0)