From aef2f530d6b0ad7e352390e6bfdd8131223c6492 Mon Sep 17 00:00:00 2001 From: Vadim Avdeev Date: Fri, 4 Oct 2019 14:14:40 +0300 Subject: [PATCH 1/2] fix(iOS): replace recursive react header search paths with non-recursive ones --- ios/RNVirgilCrypto.xcodeproj/project.pbxproj | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/ios/RNVirgilCrypto.xcodeproj/project.pbxproj b/ios/RNVirgilCrypto.xcodeproj/project.pbxproj index a49288a..f37975b 100644 --- a/ios/RNVirgilCrypto.xcodeproj/project.pbxproj +++ b/ios/RNVirgilCrypto.xcodeproj/project.pbxproj @@ -281,7 +281,8 @@ "$(inherited)", /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include, "$(SRCROOT)/../../react-native/React/**", - "$(SRCROOT)/../../../ios/Pods/Headers/**", + "$(SRCROOT)/../../../ios/Pods/Headers/Public", + "$(SRCROOT)/../../../ios/Pods/Headers/Public/React-Core", "${BUILD_DIR}/$(CONFIGURATION)$(EFFECTIVE_PLATFORM_NAME)/VirgilCrypto/VirgilCrypto.framework/Headers", "${BUILD_DIR}/$(CONFIGURATION)$(EFFECTIVE_PLATFORM_NAME)/VirgilCryptoFoundation/VirgilCryptoFoundation.framework/Headers", "${BUILD_DIR}/$(CONFIGURATION)$(EFFECTIVE_PLATFORM_NAME)/VirgilCryptoPythia/VirgilCryptoPythia.framework/Headers", @@ -307,7 +308,8 @@ "$(inherited)", /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include, "$(SRCROOT)/../../react-native/React/**", - "$(SRCROOT)/../../../ios/Pods/Headers/**", + "$(SRCROOT)/../../../ios/Pods/Headers/Public", + "$(SRCROOT)/../../../ios/Pods/Headers/Public/React-Core", "${BUILD_DIR}/$(CONFIGURATION)$(EFFECTIVE_PLATFORM_NAME)/VirgilCrypto/VirgilCrypto.framework/Headers", "${BUILD_DIR}/$(CONFIGURATION)$(EFFECTIVE_PLATFORM_NAME)/VirgilCryptoFoundation/VirgilCryptoFoundation.framework/Headers", "${BUILD_DIR}/$(CONFIGURATION)$(EFFECTIVE_PLATFORM_NAME)/VirgilCryptoPythia/VirgilCryptoPythia.framework/Headers", From 0f83098a13cbcd0a54708adca998636041832874 Mon Sep 17 00:00:00 2001 From: Vadim Avdeev Date: Fri, 4 Oct 2019 14:37:37 +0300 Subject: [PATCH 2/2] fix(calculateHash): convert result to Buffer when algorithm is not specified --- package.json | 2 +- src/virgil-crypto.js | 11 +++++++---- src/virgil-crypto.spec.js | 40 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 48 insertions(+), 5 deletions(-) create mode 100644 src/virgil-crypto.spec.js diff --git a/package.json b/package.json index a3e3212..d06b9ff 100644 --- a/package.json +++ b/package.json @@ -5,7 +5,7 @@ "main": "index.js", "typings": "index.d.ts", "scripts": { - "test": "jest src", + "test": "jest index.spec.js src", "lint": "eslint ." }, "keywords": [ diff --git a/src/virgil-crypto.js b/src/virgil-crypto.js index af5a752..5c7ca35 100644 --- a/src/virgil-crypto.js +++ b/src/virgil-crypto.js @@ -29,13 +29,16 @@ export const virgilCrypto = { calculateHash(data, algorithm) { const dataBase64 = dataToBase64(data, 'utf8', 'data'); + let response; if (algorithm == null) { - return unwrapResponse(RNVirgilCrypto.computeHash(dataBase64)); + response = RNVirgilCrypto.computeHash(dataBase64); + } else { + const nativeAlg = checkedGetHashAlgorithm(algorithm); + response = RNVirgilCrypto.computeHashWithAlgorithm(dataBase64, nativeAlg); } - - const nativeAlg = checkedGetHashAlgorithm(algorithm); + return base64ToBuffer( - unwrapResponse(RNVirgilCrypto.computeHashWithAlgorithm(dataBase64, nativeAlg)) + unwrapResponse(response) ); }, diff --git a/src/virgil-crypto.spec.js b/src/virgil-crypto.spec.js new file mode 100644 index 0000000..285acb7 --- /dev/null +++ b/src/virgil-crypto.spec.js @@ -0,0 +1,40 @@ +import { NativeModules } from 'react-native'; +import { Buffer } from 'buffer'; +import { virgilCrypto } from './virgil-crypto'; +import { HashAlgorithm } from './hash-algorithm'; + +jest.mock('react-native', () => ({ + NativeModules: { + RNVirgilCrypto: { + HashAlgorithm: { SHA256: 'SHA256'}, + computeHash: jest.fn(), + computeHashWithAlgorithm: jest.fn() + }, + }, + Platform: { + OS: 'ios', + }, +})); + +describe('virgilCrypto', () => { + beforeEach(() => { + NativeModules.RNVirgilCrypto.computeHash.mockReset(); + NativeModules.RNVirgilCrypto.computeHashWithAlgorithm.mockReset(); + }); + describe('calculateHash', () => { + it('returns Buffer when algorithm is not specified', () => { + const expectedResultBase64 = Buffer.from('hash_of_data').toString('base64'); + NativeModules.RNVirgilCrypto.computeHash.mockReturnValue({ result: expectedResultBase64 }); + const result = virgilCrypto.calculateHash('data'); + expect(Buffer.isBuffer(result)).toBe(true); + expect(result.toString('utf8')).toBe('hash_of_data'); + }); + it('returns Buffer when algorithm is specified', () => { + const expectedResultBase64 = Buffer.from('hash_of_data').toString('base64'); + NativeModules.RNVirgilCrypto.computeHashWithAlgorithm.mockReturnValue({ result: expectedResultBase64 }); + const result = virgilCrypto.calculateHash('data', HashAlgorithm.SHA256); + expect(Buffer.isBuffer(result)).toBe(true); + expect(result.toString('utf8')).toBe('hash_of_data'); + }); + }); +}); \ No newline at end of file