-
Notifications
You must be signed in to change notification settings - Fork 97
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
114 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
import type { BinaryLikeNode } from './../../../../../src/Utils'; | ||
// import { Buffer } from 'buffer'; | ||
import { Buffer } from '@craftzdog/react-native-buffer'; | ||
import crypto from 'react-native-quick-crypto'; | ||
import { describe, it } from '../../MochaRNAdapter'; | ||
import { expect } from 'chai'; | ||
|
||
type EncryptRequest = { | ||
payload: string; | ||
publicKey: ArrayBuffer; | ||
}; | ||
type EncryptResponse = { | ||
KEY: string; | ||
IV: string; | ||
PAYLOAD: string; | ||
secretKey: BinaryLikeNode; | ||
}; | ||
|
||
const encrypt = ({ payload, publicKey }: EncryptRequest): EncryptResponse => { | ||
const secretKey = crypto.randomBytes(16); | ||
const iv = crypto.randomBytes(16); | ||
|
||
const cipher = crypto.createCipheriv('aes-128-gcm', secretKey, iv); | ||
|
||
const encryptedPayload = Buffer.concat([ | ||
cipher.update(payload), | ||
cipher.final(), | ||
cipher.getAuthTag(), | ||
]).toString('base64'); | ||
|
||
const encryptedSessionKey = crypto.publicEncrypt( | ||
{ | ||
key: publicKey, | ||
padding: crypto.constants.RSA_PKCS1_OAEP_PADDING, | ||
}, | ||
secretKey | ||
); | ||
|
||
return { | ||
KEY: encryptedSessionKey.toString('base64'), | ||
IV: iv.toString('base64'), | ||
PAYLOAD: encryptedPayload, | ||
secretKey, | ||
}; | ||
}; | ||
|
||
const decrypt = ({ | ||
response, | ||
secretKey, | ||
}: { | ||
response: EncryptResponse; | ||
secretKey: BinaryLikeNode; | ||
}) => { | ||
const { IV, PAYLOAD } = response; | ||
|
||
const decipher = crypto.createDecipheriv( | ||
'aes-128-gcm', | ||
secretKey, | ||
Buffer.from(IV, 'base64') | ||
); | ||
|
||
const encryptedPayload = Buffer.from(PAYLOAD, 'base64'); | ||
let decrypted = decipher.update( | ||
Buffer.from(encryptedPayload.subarray(0, encryptedPayload.length - 16)) | ||
); | ||
decrypted = Buffer.concat([decrypted, decipher.final()]); | ||
|
||
return JSON.parse(decrypted.toString('utf8')); | ||
}; | ||
|
||
const getPublicKeyInPEMFormat = (key: string): ArrayBuffer => { | ||
return crypto | ||
.createPublicKey({ | ||
key: Buffer.from(key, 'base64'), | ||
format: 'der', | ||
type: 'spki', | ||
}) | ||
.export({ | ||
type: 'spki', | ||
format: 'pem', | ||
}); | ||
}; | ||
|
||
// export { decrypt, encrypt, getPublicKeyInPEMFormat }; | ||
|
||
describe('test398', () => { | ||
it('test398', () => { | ||
const publicKeySpkiBase64 = | ||
'MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAENlFpbMBNfCY6Lhj9A/clefyxJVIXGJ0y6CcZ/cbbyyebvN6T0aNPvpQyFdUwRtYvFHlYbqIZOM8AoqdPcnSMIA=='; | ||
|
||
const publicKey = getPublicKeyInPEMFormat(publicKeySpkiBase64); | ||
console.log({ publicKey }); | ||
const encrypted = encrypt({ | ||
payload: JSON.stringify({ a: 1 }), | ||
publicKey, | ||
}); | ||
console.log({ encrypted }); | ||
const { response: decrypted } = decrypt({ | ||
response: encrypted, | ||
secretKey: encrypted.secretKey, | ||
}); | ||
expect(decrypted).to.equal({ a: 1 }); | ||
}); | ||
}); |