|
1 | 1 | import { NativeModules } from 'react-native'; |
2 | | - |
3 | | -const { CryptoAlgorithm } = NativeModules; |
4 | | - |
5 | | -export default class Crypto { |
6 | | - static hashSHA256 = (value: string) => { |
7 | | - return CryptoAlgorithm.hashSHA256(value); |
8 | | - }; |
9 | | - |
10 | | - static encryptAES = (value: string, secretKey: string, ivKey?: string) => { |
11 | | - return CryptoAlgorithm.encryptAES(value, secretKey, ivKey); |
12 | | - }; |
13 | | - |
14 | | - static decryptAES = (value: string, secretKey: string, ivKey?: string) => { |
15 | | - return CryptoAlgorithm.decryptAES(value, secretKey, ivKey); |
16 | | - }; |
17 | | - |
18 | | - static genRSAKeyPair = () => { |
19 | | - return CryptoAlgorithm.genRSAKeyPair(); |
20 | | - }; |
21 | | - |
22 | | - static encryptRSA = (value: string, publicKey: string) => { |
23 | | - return CryptoAlgorithm.encryptRSA(value, publicKey); |
24 | | - }; |
25 | | - |
26 | | - static decryptRSA = (value: any, privateKey: string) => { |
27 | | - return CryptoAlgorithm.decryptRSA(value, privateKey); |
28 | | - }; |
29 | | - |
30 | | - static genHmacSecretKey = () => { |
31 | | - return CryptoAlgorithm.genSecretKey('Hmac'); |
32 | | - }; |
33 | | - |
34 | | - static encryptHmacAes = (value: string, privateKey: string) => { |
35 | | - return CryptoAlgorithm.encryptHmacAes(value, privateKey); |
36 | | - }; |
37 | | - |
38 | | - static decryptHmacAes = (value: any, privateKey: string) => { |
39 | | - return CryptoAlgorithm.decryptHmacAes(value, privateKey); |
40 | | - }; |
41 | | - |
42 | | - static verifyHmac = (value: string, privateKey: string) => { |
43 | | - return CryptoAlgorithm.verifyHmac(value, privateKey); |
44 | | - }; |
| 2 | +import { CryptoAlgorithm } from './CryptoAlgorithm'; |
| 3 | + |
| 4 | +const { CryptoAlgorithm: NativeCryptoAlgorithm } = NativeModules; |
| 5 | + |
| 6 | + |
| 7 | +export default class Crypto implements CryptoAlgorithm{ |
| 8 | + // Instance method to hash SHA256 |
| 9 | + hashSHA256(value: string): Promise<string | null> { |
| 10 | + return NativeCryptoAlgorithm.hashSHA256(value); |
| 11 | + } |
| 12 | + |
| 13 | + // Instance method to encrypt using AES |
| 14 | + encryptAES(value: string, secretKey: string, ivKey?: string): Promise<string | null> { |
| 15 | + return NativeCryptoAlgorithm.encryptAES(value, secretKey, ivKey); |
| 16 | + } |
| 17 | + |
| 18 | + // Instance method to decrypt using AES |
| 19 | + decryptAES(value: string, secretKey: string, ivKey?: string): Promise<string | null> { |
| 20 | + return NativeCryptoAlgorithm.decryptAES(value, secretKey, ivKey); |
| 21 | + } |
| 22 | + |
| 23 | + // Instance method to generate RSA key pair |
| 24 | + genRSAKeyPair(): Promise<any> { |
| 25 | + return NativeCryptoAlgorithm.genRSAKeyPair(); |
| 26 | + } |
| 27 | + |
| 28 | + // Instance method to encrypt using RSA |
| 29 | + encryptRSA(value: string, publicKey: string): Promise<string | null> { |
| 30 | + return NativeCryptoAlgorithm.encryptRSA(value, publicKey); |
| 31 | + } |
| 32 | + |
| 33 | + // Instance method to decrypt using RSA |
| 34 | + decryptRSA(value: any, privateKey: string): Promise<string | null> { |
| 35 | + return NativeCryptoAlgorithm.decryptRSA(value, privateKey); |
| 36 | + } |
| 37 | + |
| 38 | + // Instance method to generate HMAC secret key |
| 39 | + genHmacSecretKey(): Promise<null> { |
| 40 | + return NativeCryptoAlgorithm.genSecretKey('Hmac'); |
| 41 | + } |
| 42 | + |
| 43 | + // Instance method to encrypt using HMAC AES |
| 44 | + encryptHmacAes(value: string, privateKey: string): Promise<string | null> { |
| 45 | + return NativeCryptoAlgorithm.encryptHmacAes(value, privateKey); |
| 46 | + } |
| 47 | + |
| 48 | + // Instance method to decrypt using HMAC AES |
| 49 | + decryptHmacAes(value: any, privateKey: string): Promise<string | null> { |
| 50 | + return NativeCryptoAlgorithm.decryptHmacAes(value, privateKey); |
| 51 | + } |
| 52 | + |
| 53 | + // Instance method to verify HMAC |
| 54 | + verifyHmac(value: string, privateKey: string): Promise<string | null> { |
| 55 | + return NativeCryptoAlgorithm.verifyHmac(value, privateKey); |
| 56 | + } |
45 | 57 | } |
0 commit comments