|
| 1 | +import type { BinaryLikeNode } from './../../../../../src/Utils'; |
| 2 | +// import { Buffer } from 'buffer'; |
| 3 | +import { Buffer } from '@craftzdog/react-native-buffer'; |
| 4 | +import crypto from 'react-native-quick-crypto'; |
| 5 | +import { describe, it } from '../../MochaRNAdapter'; |
| 6 | +import { expect } from 'chai'; |
| 7 | + |
| 8 | +type EncryptRequest = { |
| 9 | + payload: string; |
| 10 | + publicKey: ArrayBuffer; |
| 11 | +}; |
| 12 | +type EncryptResponse = { |
| 13 | + KEY: string; |
| 14 | + IV: string; |
| 15 | + PAYLOAD: string; |
| 16 | + secretKey: BinaryLikeNode; |
| 17 | +}; |
| 18 | + |
| 19 | +const encrypt = ({ payload, publicKey }: EncryptRequest): EncryptResponse => { |
| 20 | + const secretKey = crypto.randomBytes(16); |
| 21 | + const iv = crypto.randomBytes(16); |
| 22 | + |
| 23 | + const cipher = crypto.createCipheriv('aes-128-gcm', secretKey, iv); |
| 24 | + |
| 25 | + const encryptedPayload = Buffer.concat([ |
| 26 | + cipher.update(payload), |
| 27 | + cipher.final(), |
| 28 | + cipher.getAuthTag(), |
| 29 | + ]).toString('base64'); |
| 30 | + |
| 31 | + const encryptedSessionKey = crypto.publicEncrypt( |
| 32 | + { |
| 33 | + key: publicKey, |
| 34 | + padding: crypto.constants.RSA_PKCS1_OAEP_PADDING, |
| 35 | + }, |
| 36 | + secretKey |
| 37 | + ); |
| 38 | + |
| 39 | + return { |
| 40 | + KEY: encryptedSessionKey.toString('base64'), |
| 41 | + IV: iv.toString('base64'), |
| 42 | + PAYLOAD: encryptedPayload, |
| 43 | + secretKey, |
| 44 | + }; |
| 45 | +}; |
| 46 | + |
| 47 | +const decrypt = ({ |
| 48 | + response, |
| 49 | + secretKey, |
| 50 | +}: { |
| 51 | + response: EncryptResponse; |
| 52 | + secretKey: BinaryLikeNode; |
| 53 | +}) => { |
| 54 | + const { IV, PAYLOAD } = response; |
| 55 | + |
| 56 | + const decipher = crypto.createDecipheriv( |
| 57 | + 'aes-128-gcm', |
| 58 | + secretKey, |
| 59 | + Buffer.from(IV, 'base64') |
| 60 | + ); |
| 61 | + |
| 62 | + const encryptedPayload = Buffer.from(PAYLOAD, 'base64'); |
| 63 | + let decrypted = decipher.update( |
| 64 | + Buffer.from(encryptedPayload.subarray(0, encryptedPayload.length - 16)) |
| 65 | + ); |
| 66 | + decrypted = Buffer.concat([decrypted, decipher.final()]); |
| 67 | + |
| 68 | + return JSON.parse(decrypted.toString('utf8')); |
| 69 | +}; |
| 70 | + |
| 71 | +const getPublicKeyInPEMFormat = (key: string): ArrayBuffer => { |
| 72 | + return crypto |
| 73 | + .createPublicKey({ |
| 74 | + key: Buffer.from(key, 'base64'), |
| 75 | + format: 'der', |
| 76 | + type: 'spki', |
| 77 | + }) |
| 78 | + .export({ |
| 79 | + type: 'spki', |
| 80 | + format: 'pem', |
| 81 | + }); |
| 82 | +}; |
| 83 | + |
| 84 | +// export { decrypt, encrypt, getPublicKeyInPEMFormat }; |
| 85 | + |
| 86 | +describe('test398', () => { |
| 87 | + it('test398', () => { |
| 88 | + const publicKeySpkiBase64 = |
| 89 | + 'MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAENlFpbMBNfCY6Lhj9A/clefyxJVIXGJ0y6CcZ/cbbyyebvN6T0aNPvpQyFdUwRtYvFHlYbqIZOM8AoqdPcnSMIA=='; |
| 90 | + |
| 91 | + const publicKey = getPublicKeyInPEMFormat(publicKeySpkiBase64); |
| 92 | + console.log({ publicKey }); |
| 93 | + const encrypted = encrypt({ |
| 94 | + payload: JSON.stringify({ a: 1 }), |
| 95 | + publicKey, |
| 96 | + }); |
| 97 | + console.log({ encrypted }); |
| 98 | + const { response: decrypted } = decrypt({ |
| 99 | + response: encrypted, |
| 100 | + secretKey: encrypted.secretKey, |
| 101 | + }); |
| 102 | + expect(decrypted).to.equal({ a: 1 }); |
| 103 | + }); |
| 104 | +}); |
0 commit comments