Skip to content

Commit

Permalink
Merge pull request #9 from VirgilSecurity/fix-hash
Browse files Browse the repository at this point in the history
Fixes to `calculateHash` and `HEADER_SEARCH_PATHS`
  • Loading branch information
vadimavdeev authored Oct 4, 2019
2 parents 7d45a5d + 0f83098 commit 86636aa
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 7 deletions.
6 changes: 4 additions & 2 deletions ios/RNVirgilCrypto.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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",
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"main": "index.js",
"typings": "index.d.ts",
"scripts": {
"test": "jest src",
"test": "jest index.spec.js src",
"lint": "eslint ."
},
"keywords": [
Expand Down
11 changes: 7 additions & 4 deletions src/virgil-crypto.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
);
},

Expand Down
40 changes: 40 additions & 0 deletions src/virgil-crypto.spec.js
Original file line number Diff line number Diff line change
@@ -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');
});
});
});

0 comments on commit 86636aa

Please sign in to comment.