@@ -2,137 +2,141 @@ import { NativeModules } from 'react-native';
22
33NativeModules . CryptoAlgorithm = {
44 hashSHA256 : jest . fn ( ) ,
5+ encryptAES : jest . fn ( ) ,
6+ decryptAES : jest . fn ( ) ,
7+ genRSAKeyPair : jest . fn ( ) ,
8+ encryptRSA : jest . fn ( ) ,
9+ decryptRSA : jest . fn ( ) ,
10+ genHmacSecretKey : jest . fn ( ) ,
11+ encryptHmacAes : jest . fn ( ) ,
12+ decryptHmacAes : jest . fn ( ) ,
13+ verifyHmac : jest . fn ( ) ,
514} ;
615
7- // jest.mock('react-native-crypto-algorithm', () => ({
8- // hashSHA256: jest.fn(),
9- // encryptAES: jest.fn(),
10- // decryptAES: jest.fn(),
11- // genRSAKeyPair: jest.fn(),
12- // encryptRSA: jest.fn(),
13- // decryptRSA: jest.fn(),
14- // genHmacSecretKey: jest.fn(),
15- // encryptHmacAes: jest.fn(),
16- // decryptHmacAes: jest.fn(),
17- // verifyHmac: jest.fn(),
18- // }));
19-
2016describe ( 'Crypto' , ( ) => {
2117 beforeEach ( ( ) => {
2218 NativeModules . CryptoAlgorithm . hashSHA256 . mockReset ( ) ;
19+ NativeModules . CryptoAlgorithm . encryptAES . mockReset ( ) ;
20+ NativeModules . CryptoAlgorithm . decryptAES . mockReset ( ) ;
21+ NativeModules . CryptoAlgorithm . genRSAKeyPair . mockReset ( ) ;
22+ NativeModules . CryptoAlgorithm . encryptRSA . mockReset ( ) ;
23+ NativeModules . CryptoAlgorithm . decryptRSA . mockReset ( ) ;
24+ NativeModules . CryptoAlgorithm . genHmacSecretKey . mockReset ( ) ;
25+ NativeModules . CryptoAlgorithm . encryptHmacAes . mockReset ( ) ;
26+ NativeModules . CryptoAlgorithm . decryptHmacAes . mockReset ( ) ;
27+ NativeModules . CryptoAlgorithm . verifyHmac . mockReset ( ) ;
2328 } ) ;
2429
2530 test ( 'should hash value using SHA256' , async ( ) => {
2631 const mockValue = 'Hello123' ;
2732 const mockHash = '134563d4e440f0e418b0f382f23a2cf301af6d7f648ccfae9895018345d779a3' ;
2833 ( NativeModules . CryptoAlgorithm . hashSHA256 as jest . Mock ) . mockResolvedValue ( mockHash ) ; // Mock the function return value
29- // NativeModules.CryptoAlgorithm.hashSHA256.mockResolvedValue(mockHash);
3034
3135 const result = await NativeModules . CryptoAlgorithm . hashSHA256 ( mockValue ) ; // Call the function
3236 expect ( NativeModules . CryptoAlgorithm . hashSHA256 ) . toHaveBeenCalledWith ( mockValue ) ; // Verify the function is called with the correct value
3337 expect ( result ) . toBe ( mockHash ) ; // Verify the result
3438 } ) ;
3539
36- // it('should encrypt value using AES', async () => {
37- // const mockValue = 'Hello123';
38- // const mockSecretKey = 'Alo123';
39- // const mockEncrypted = 'KYd01TEkRcK+U0mVYaB5AA==';
40- // (Crypto .encryptAES as jest.Mock).mockResolvedValue(mockEncrypted);
41- //
42- // const result = await Crypto .encryptAES(mockValue, mockSecretKey);
43- // expect(Crypto .encryptAES).toHaveBeenCalledWith(mockValue, mockSecretKey);
44- // expect(result).toBe(mockEncrypted);
45- // });
46- //
47- // it('should decrypt value using AES', async () => {
48- // const mockSecretKey = 'Alo123';
49- // const mockDecrypted = 'Hello123';
50- // const mockEncrypted = 'KYd01TEkRcK+U0mVYaB5AA==';
51- // (Crypto .decryptAES as jest.Mock).mockResolvedValue(mockDecrypted);
52- //
53- // const result = await Crypto .decryptAES(mockEncrypted, mockSecretKey);
54- // expect(Crypto .decryptAES).toHaveBeenCalledWith(mockEncrypted, mockSecretKey);
55- // expect(result).toBe(mockDecrypted);
56- // });
57- //
58- // it('should generate RSA key pair', async () => {
59- // const mockKeyPair = { publicKey: 'mockPublicKey', privateKey: 'mockPrivateKey' };
60- // (Crypto .genRSAKeyPair as jest.Mock).mockResolvedValue(mockKeyPair);
61- //
62- // const result = await Crypto .genRSAKeyPair();
63- // expect(Crypto .genRSAKeyPair).toHaveBeenCalled();
64- // expect(result).toBe(mockKeyPair);
65- // });
66- //
67- // it('should encrypt value using RSA', async () => {
68- // const mockPublicKey = 'mockPublicKey';
69- // const mockData = 'Hello123';
70- // const mockEncryptedData = 'eInm/VQLZM9wK7H+w8jqMeiwab6xZFx6AYSRSramC5o/k2eHbJ+9NkAM68/tYFNvrG03N3WXxIke\n' +
71- // 'J/AF5TbGsdgmlTsOfjnKi88ci24LCRhED0CYgJT0SNwVC4NcubMV5QbKbcdJO0S+2mMkPD3qzB83\n' +
72- // '055GPlmRr4FJtGJ44ASM4W9ZGahwq32MT6VK04DBq/LGXj3bJZ8v7M7273lTYOqGCFHc+X8WOGcB\n' +
73- // '1NNhhxxYbsRzLpEaK7Wa8Sv+CavpK7TwUgVVlCwL9G4WGEmCCMDDnKXH2pzspDumMOUaDUiPg005\n' +
74- // '73tRWXaskPZtIk1KfDwxnRMz7rFXMiNvjU+pCA==';
75- // (Crypto .encryptRSA as jest.Mock).mockResolvedValue(mockEncryptedData);
76- //
77- // const result = await Crypto .encryptRSA(mockData, mockPublicKey);
78- // expect(Crypto .encryptRSA).toHaveBeenCalledWith(mockData, mockPublicKey);
79- // expect(result).toBe(mockEncryptedData);
80- // });
81- //
82- // it('should decrypt value using RSA', async () => {
83- // const mockPrivateKey = 'mockPrivateKey';
84- // const mockDecrypt = 'Hello123';
85- // const mockEncryptedData = 'eInm/VQLZM9wK7H+w8jqMeiwab6xZFx6AYSRSramC5o/k2eHbJ+9NkAM68/tYFNvrG03N3WXxIke\n' +
86- // 'J/AF5TbGsdgmlTsOfjnKi88ci24LCRhED0CYgJT0SNwVC4NcubMV5QbKbcdJO0S+2mMkPD3qzB83\n' +
87- // '055GPlmRr4FJtGJ44ASM4W9ZGahwq32MT6VK04DBq/LGXj3bJZ8v7M7273lTYOqGCFHc+X8WOGcB\n' +
88- // '1NNhhxxYbsRzLpEaK7Wa8Sv+CavpK7TwUgVVlCwL9G4WGEmCCMDDnKXH2pzspDumMOUaDUiPg005\n' +
89- // '73tRWXaskPZtIk1KfDwxnRMz7rFXMiNvjU+pCA==';
90- // (Crypto .decryptRSA as jest.Mock).mockResolvedValue(mockDecrypt);
91- //
92- // const result = await Crypto .decryptRSA(mockEncryptedData, mockPrivateKey);
93- // expect(Crypto .decryptRSA).toHaveBeenCalledWith(mockEncryptedData, mockPrivateKey);
94- // expect(result).toBe(mockDecrypt);
95- // });
96- //
97- // it('should generate HMAC secret key', async () => {
98- // const mockSecretKey = 'mockedHmacSecretKey';
99- // (Crypto .genHmacSecretKey as jest.Mock).mockResolvedValue(mockSecretKey);
100- //
101- // const result = await Crypto .genHmacSecretKey();
102- // expect(Crypto .genHmacSecretKey).toHaveBeenCalled();
103- // expect(result).toBe(mockSecretKey);
104- // });
105- //
106- // it('should encrypt value using HMAC AES', async () => {
107- // const mockData = 'Hello123';
108- // const mockPrivateKey = '4PVi7C94nl+u3B4Gnmjsgf69crr/+HBJhOps2+3xB+M=';
109- // const mockEncryptedHmacAES = 'qUisMsd7to7zcm5y4/idNw==';
110- // (Crypto .encryptHmacAes as jest.Mock).mockResolvedValue(mockEncryptedHmacAES);
111- //
112- // const result = await Crypto .encryptHmacAes(mockData, mockPrivateKey);
113- // expect(Crypto .encryptHmacAes).toHaveBeenCalledWith(mockData, mockPrivateKey);
114- // expect(result).toBe(mockEncryptedHmacAES);
115- // });
116- //
117- // it('should decrypt value using HMAC AES', async () => {
118- // const mockDecrypt = 'Hello123';
119- // const mockPrivateKey = '4PVi7C94nl+u3B4Gnmjsgf69crr/+HBJhOps2+3xB+M=';
120- // const mockEncryptedHmacAES = 'qUisMsd7to7zcm5y4/idNw==';
121- // (Crypto .decryptHmacAes as jest.Mock).mockResolvedValue(mockDecrypt);
122- //
123- // const result = await Crypto .decryptHmacAes(mockEncryptedHmacAES, mockPrivateKey);
124- // expect(Crypto .decryptHmacAes).toHaveBeenCalledWith(mockEncryptedHmacAES, mockPrivateKey);
125- // expect(result).toBe(mockDecrypt);
126- // });
127- //
128- // it('should verify HMAC', async () => {
129- // const mockData = 'Hello123';
130- // const mockPrivateKey = '4PVi7C94nl+u3B4Gnmjsgf69crr/+HBJhOps2+3xB+M=';
131- // const mockVerifiedHmac = true;
132- // (Crypto .verifyHmac as jest.Mock).mockResolvedValue(mockVerifiedHmac);
133- //
134- // const result = await Crypto .verifyHmac(mockData, mockPrivateKey);
135- // expect(Crypto .verifyHmac).toHaveBeenCalledWith(mockData, mockPrivateKey);
136- // expect(result).toBe(mockVerifiedHmac);
137- // });
40+ it ( 'should encrypt value using AES' , async ( ) => {
41+ const mockValue = 'Hello123' ;
42+ const mockSecretKey = 'Alo123' ;
43+ const mockEncrypted = 'KYd01TEkRcK+U0mVYaB5AA==' ;
44+ ( NativeModules . CryptoAlgorithm . encryptAES as jest . Mock ) . mockResolvedValue ( mockEncrypted ) ;
45+
46+ const result = await NativeModules . CryptoAlgorithm . encryptAES ( mockValue , mockSecretKey ) ;
47+ expect ( NativeModules . CryptoAlgorithm . encryptAES ) . toHaveBeenCalledWith ( mockValue , mockSecretKey ) ;
48+ expect ( result ) . toBe ( mockEncrypted ) ;
49+ } ) ;
50+
51+ it ( 'should decrypt value using AES' , async ( ) => {
52+ const mockSecretKey = 'Alo123' ;
53+ const mockDecrypted = 'Hello123' ;
54+ const mockEncrypted = 'KYd01TEkRcK+U0mVYaB5AA==' ;
55+ ( NativeModules . CryptoAlgorithm . decryptAES as jest . Mock ) . mockResolvedValue ( mockDecrypted ) ;
56+
57+ const result = await NativeModules . CryptoAlgorithm . decryptAES ( mockEncrypted , mockSecretKey ) ;
58+ expect ( NativeModules . CryptoAlgorithm . decryptAES ) . toHaveBeenCalledWith ( mockEncrypted , mockSecretKey ) ;
59+ expect ( result ) . toBe ( mockDecrypted ) ;
60+ } ) ;
61+
62+ it ( 'should generate RSA key pair' , async ( ) => {
63+ const mockKeyPair = { publicKey : 'mockPublicKey' , privateKey : 'mockPrivateKey' } ;
64+ ( NativeModules . CryptoAlgorithm . genRSAKeyPair as jest . Mock ) . mockResolvedValue ( mockKeyPair ) ;
65+
66+ const result = await NativeModules . CryptoAlgorithm . genRSAKeyPair ( ) ;
67+ expect ( NativeModules . CryptoAlgorithm . genRSAKeyPair ) . toHaveBeenCalled ( ) ;
68+ expect ( result ) . toBe ( mockKeyPair ) ;
69+ } ) ;
70+
71+ it ( 'should encrypt value using RSA' , async ( ) => {
72+ const mockPublicKey = 'mockPublicKey' ;
73+ const mockData = 'Hello123' ;
74+ const mockEncryptedData = 'eInm/VQLZM9wK7H+w8jqMeiwab6xZFx6AYSRSramC5o/k2eHbJ+9NkAM68/tYFNvrG03N3WXxIke\n' +
75+ 'J/AF5TbGsdgmlTsOfjnKi88ci24LCRhED0CYgJT0SNwVC4NcubMV5QbKbcdJO0S+2mMkPD3qzB83\n' +
76+ '055GPlmRr4FJtGJ44ASM4W9ZGahwq32MT6VK04DBq/LGXj3bJZ8v7M7273lTYOqGCFHc+X8WOGcB\n' +
77+ '1NNhhxxYbsRzLpEaK7Wa8Sv+CavpK7TwUgVVlCwL9G4WGEmCCMDDnKXH2pzspDumMOUaDUiPg005\n' +
78+ '73tRWXaskPZtIk1KfDwxnRMz7rFXMiNvjU+pCA==' ;
79+ ( NativeModules . CryptoAlgorithm . encryptRSA as jest . Mock ) . mockResolvedValue ( mockEncryptedData ) ;
80+
81+ const result = await NativeModules . CryptoAlgorithm . encryptRSA ( mockData , mockPublicKey ) ;
82+ expect ( NativeModules . CryptoAlgorithm . encryptRSA ) . toHaveBeenCalledWith ( mockData , mockPublicKey ) ;
83+ expect ( result ) . toBe ( mockEncryptedData ) ;
84+ } ) ;
85+
86+ it ( 'should decrypt value using RSA' , async ( ) => {
87+ const mockPrivateKey = 'mockPrivateKey' ;
88+ const mockDecrypt = 'Hello123' ;
89+ const mockEncryptedData = 'eInm/VQLZM9wK7H+w8jqMeiwab6xZFx6AYSRSramC5o/k2eHbJ+9NkAM68/tYFNvrG03N3WXxIke\n' +
90+ 'J/AF5TbGsdgmlTsOfjnKi88ci24LCRhED0CYgJT0SNwVC4NcubMV5QbKbcdJO0S+2mMkPD3qzB83\n' +
91+ '055GPlmRr4FJtGJ44ASM4W9ZGahwq32MT6VK04DBq/LGXj3bJZ8v7M7273lTYOqGCFHc+X8WOGcB\n' +
92+ '1NNhhxxYbsRzLpEaK7Wa8Sv+CavpK7TwUgVVlCwL9G4WGEmCCMDDnKXH2pzspDumMOUaDUiPg005\n' +
93+ '73tRWXaskPZtIk1KfDwxnRMz7rFXMiNvjU+pCA==' ;
94+ ( NativeModules . CryptoAlgorithm . decryptRSA as jest . Mock ) . mockResolvedValue ( mockDecrypt ) ;
95+
96+ const result = await NativeModules . CryptoAlgorithm . decryptRSA ( mockEncryptedData , mockPrivateKey ) ;
97+ expect ( NativeModules . CryptoAlgorithm . decryptRSA ) . toHaveBeenCalledWith ( mockEncryptedData , mockPrivateKey ) ;
98+ expect ( result ) . toBe ( mockDecrypt ) ;
99+ } ) ;
100+
101+ it ( 'should generate HMAC secret key' , async ( ) => {
102+ const mockSecretKey = 'mockedHmacSecretKey' ;
103+ ( NativeModules . CryptoAlgorithm . genHmacSecretKey as jest . Mock ) . mockResolvedValue ( mockSecretKey ) ;
104+
105+ const result = await NativeModules . CryptoAlgorithm . genHmacSecretKey ( ) ;
106+ expect ( NativeModules . CryptoAlgorithm . genHmacSecretKey ) . toHaveBeenCalled ( ) ;
107+ expect ( result ) . toBe ( mockSecretKey ) ;
108+ } ) ;
109+
110+ it ( 'should encrypt value using HMAC AES' , async ( ) => {
111+ const mockData = 'Hello123' ;
112+ const mockPrivateKey = '4PVi7C94nl+u3B4Gnmjsgf69crr/+HBJhOps2+3xB+M=' ;
113+ const mockEncryptedHmacAES = 'qUisMsd7to7zcm5y4/idNw==' ;
114+ ( NativeModules . CryptoAlgorithm . encryptHmacAes as jest . Mock ) . mockResolvedValue ( mockEncryptedHmacAES ) ;
115+
116+ const result = await NativeModules . CryptoAlgorithm . encryptHmacAes ( mockData , mockPrivateKey ) ;
117+ expect ( NativeModules . CryptoAlgorithm . encryptHmacAes ) . toHaveBeenCalledWith ( mockData , mockPrivateKey ) ;
118+ expect ( result ) . toBe ( mockEncryptedHmacAES ) ;
119+ } ) ;
120+
121+ it ( 'should decrypt value using HMAC AES' , async ( ) => {
122+ const mockDecrypt = 'Hello123' ;
123+ const mockPrivateKey = '4PVi7C94nl+u3B4Gnmjsgf69crr/+HBJhOps2+3xB+M=' ;
124+ const mockEncryptedHmacAES = 'qUisMsd7to7zcm5y4/idNw==' ;
125+ ( NativeModules . CryptoAlgorithm . decryptHmacAes as jest . Mock ) . mockResolvedValue ( mockDecrypt ) ;
126+
127+ const result = await NativeModules . CryptoAlgorithm . decryptHmacAes ( mockEncryptedHmacAES , mockPrivateKey ) ;
128+ expect ( NativeModules . CryptoAlgorithm . decryptHmacAes ) . toHaveBeenCalledWith ( mockEncryptedHmacAES , mockPrivateKey ) ;
129+ expect ( result ) . toBe ( mockDecrypt ) ;
130+ } ) ;
131+
132+ it ( 'should verify HMAC' , async ( ) => {
133+ const mockData = 'Hello123' ;
134+ const mockPrivateKey = '4PVi7C94nl+u3B4Gnmjsgf69crr/+HBJhOps2+3xB+M=' ;
135+ const mockVerifiedHmac = true ;
136+ ( NativeModules . CryptoAlgorithm . verifyHmac as jest . Mock ) . mockResolvedValue ( mockVerifiedHmac ) ;
137+
138+ const result = await NativeModules . CryptoAlgorithm . verifyHmac ( mockData , mockPrivateKey ) ;
139+ expect ( NativeModules . CryptoAlgorithm . verifyHmac ) . toHaveBeenCalledWith ( mockData , mockPrivateKey ) ;
140+ expect ( result ) . toBe ( mockVerifiedHmac ) ;
141+ } ) ;
138142} ) ;
0 commit comments