Skip to content

Commit f7dad5d

Browse files
authored
Merge pull request #25 from cjthompson/fix-android-sha-hex
Android: Change SHA hash of a string to match iOS
2 parents 9d979ff + eaaa303 commit f7dad5d

2 files changed

Lines changed: 51 additions & 23 deletions

File tree

README.md

Lines changed: 50 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -118,44 +118,43 @@ Testing [repository](https://github.com/ghbutton/react-native-simple-crypto-test
118118
```javascript
119119
import RNSimpleCrypto from "react-native-simple-crypto";
120120

121+
const toHex = RNSimpleCrypto.utils.convertArrayBufferToHex
122+
const toUtf8 = RNSimpleCrypto.utils.convertArrayBufferToUtf8
123+
121124
// -- AES ------------------------------------------------------------- //
122125
const message = "data to encrypt";
123126
const messageArrayBuffer = RNSimpleCrypto.utils.convertUtf8ToArrayBuffer(
124127
message
125128
);
126129

127130
const keyArrayBuffer = await RNSimpleCrypto.utils.randomBytes(32);
128-
console.log("randomBytes key", keyArrayBuffer);
131+
console.log("randomBytes key", toHex(keyArrayBuffer));
129132

130133
const ivArrayBuffer = await RNSimpleCrypto.utils.randomBytes(16);
131-
console.log("randomBytes iv", ivArrayBuffer);
134+
console.log("randomBytes iv", toHex(ivArrayBuffer));
132135

133136
const cipherTextArrayBuffer = await RNSimpleCrypto.AES.encrypt(
134137
messageArrayBuffer,
135138
keyArrayBuffer,
136139
ivArrayBuffer
137140
);
138-
console.log("AES encrypt", cipherTextArrayBuffer);
141+
console.log("AES encrypt", toHex(cipherTextArrayBuffer))
139142

140143
const decryptedArrayBuffer = await RNSimpleCrypto.AES.decrypt(
141144
cipherTextArrayBuffer,
142145
keyArrayBuffer,
143146
ivArrayBuffer
144147
);
145-
const decrypted = RNSimpleCrypto.utils.convertArrayBufferToUtf8(
146-
decryptedArrayBuffer
147-
);
148-
console.log("AES decrypt", decrypted);
148+
console.log("AES decrypt", toUtf8(decryptedArrayBuffer));
149+
if (toUtf8(decryptedArrayBuffer) !== message) {
150+
console.error('AES decrypt returned unexpected results')
151+
}
149152

150153
// -- HMAC ------------------------------------------------------------ //
151154

152155
const keyHmac = await RNSimpleCrypto.utils.randomBytes(32);
153-
const signatureArrayBuffer = await RNSimpleCrypto.HMAC.hmac256(message, keyHmac);
154-
155-
const signatureHex = RNSimpleCrypto.utils.convertArrayBufferToHex(
156-
signatureArrayBuffer
157-
);
158-
console.log("HMAC signature", signatureHex);
156+
const signatureArrayBuffer = await RNSimpleCrypto.HMAC.hmac256(messageArrayBuffer, keyHmac);
157+
console.log("HMAC signature", toHex(signatureArrayBuffer));
159158

160159
// -- SHA ------------------------------------------------------------- //
161160

@@ -168,18 +167,29 @@ console.log("SHA256 hash", sha256Hash);
168167
const sha512Hash = await RNSimpleCrypto.SHA.sha512("test");
169168
console.log("SHA512 hash", sha512Hash);
170169

171-
const dataToHash = await RNSimpleCrypto.utils.randomBytes(64);
172-
const sha1ArrayBuffer = await RNSimpleCrypto.SHA.sha1(dataToHash);
173-
console.log('SHA256 hash bytes', sha1ArrayBuffer);
170+
const arrayBufferToHash = RNSimpleCrypto.utils.convertUtf8ToArrayBuffer("test");
171+
const sha1ArrayBuffer = await RNSimpleCrypto.SHA.sha1(arrayBufferToHash);
172+
console.log('SHA1 hash bytes', toHex(sha1ArrayBuffer));
173+
if (toHex(sha1ArrayBuffer) !== sha1Hash) {
174+
console.error('SHA1 result mismatch!')
175+
}
174176

175-
const sha256ArrayBuffer = await RNSimpleCrypto.SHA.sha256(dataToHash);
176-
console.log('SHA256 hash bytes', sha256ArrayBuffer);
177+
const sha256ArrayBuffer = await RNSimpleCrypto.SHA.sha256(arrayBufferToHash);
178+
console.log('SHA256 hash bytes', toHex(sha256ArrayBuffer));
179+
if (toHex(sha256ArrayBuffer) !== sha256Hash) {
180+
console.error('SHA256 result mismatch!')
181+
}
177182

183+
const sha512ArrayBuffer = await RNSimpleCrypto.SHA.sha512(arrayBufferToHash);
184+
console.log('SHA512 hash bytes', toHex(sha512ArrayBuffer));
185+
if (toHex(sha512ArrayBuffer) !== sha512Hash) {
186+
console.error('SHA512 result mismatch!')
187+
}
178188

179189
// -- PBKDF2 ---------------------------------------------------------- //
180190

181191
const password = "secret password";
182-
const salt = RNSimpleCrypto.utils.randomBytes(8);
192+
const salt = "my-salt"
183193
const iterations = 4096;
184194
const keyInBytes = 32;
185195
const hash = "SHA1";
@@ -190,9 +200,23 @@ const passwordKey = await RNSimpleCrypto.PBKDF2.hash(
190200
keyInBytes,
191201
hash
192202
);
193-
console.log("PBKDF2 passwordKey", passwordKey);
203+
console.log("PBKDF2 passwordKey", toHex(passwordKey));
204+
205+
const passwordKeyArrayBuffer = await RNSimpleCrypto.PBKDF2.hash(
206+
RNSimpleCrypto.utils.convertUtf8ToArrayBuffer(password),
207+
RNSimpleCrypto.utils.convertUtf8ToArrayBuffer(salt),
208+
iterations,
209+
keyInBytes,
210+
hash
211+
);
212+
console.log("PBKDF2 passwordKey bytes", toHex(passwordKeyArrayBuffer));
213+
214+
if (toHex(passwordKeyArrayBuffer) !== toHex(passwordKey)) {
215+
console.error('PBKDF2 result mismatch!')
216+
}
217+
194218
const password2 = messageArrayBuffer;
195-
const salt2 = RNSimpleCrypto.utils.randomBytes(8);
219+
const salt2 = await RNSimpleCrypto.utils.randomBytes(8);
196220
const iterations2 = 10000;
197221
const keyInBytes2 = 32;
198222
const hash2 = "SHA256";
@@ -204,7 +228,8 @@ const passwordKey2 = await RNSimpleCrypto.PBKDF2.hash(
204228
keyInBytes2,
205229
hash2
206230
);
207-
console.log("PBKDF2 passwordKey", passwordKey2);
231+
console.log("PBKDF2 passwordKey2", toHex(passwordKey2));
232+
208233

209234
// -- RSA ------------------------------------------------------------ //
210235

@@ -238,6 +263,9 @@ const rsaDecryptedMessage = await RNSimpleCrypto.RSA.decrypt(
238263
rsaKeys.private
239264
);
240265
console.log("rsa Decrypt:", rsaDecryptedMessage);
266+
if (rsaDecryptedMessage !== message ) {
267+
console.error('RSA decrypt returned unexpected result')
268+
}
241269
```
242270

243271
## Forked Libraries

android/src/main/java/com/pedrouid/crypto/RCTSha.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ public void shaBase64(String data, String algorithm, Promise promise) throws Exc
8787
public void shaUtf8(String data, String algorithm, Promise promise) throws Exception {
8888
try {
8989
byte[] digest = this.sha(data.getBytes(), algorithm);
90-
promise.resolve(Base64.encodeToString(digest, Base64.DEFAULT));
90+
promise.resolve(Util.bytesToHex(digest));
9191
} catch (Exception e) {
9292
promise.reject("-1", e.getMessage());
9393
}

0 commit comments

Comments
 (0)