2021-10-03 17:00:49 +02:00
import { rsa } from './ppk' ;
interface TestData {
publicKey : string ;
privateKey : string ;
plaintext : string ;
ciphertext : string ;
}
// This is conveninent to quickly generate some data to verify for example that
// react-native-rsa can decrypt data from node-rsa and vice-versa.
export async function createTestData() {
const plaintext = 'just testing' ;
const keyPair = await rsa ( ) . generateKeyPair ( 2048 ) ;
const ciphertext = await rsa ( ) . encrypt ( plaintext , keyPair ) ;
return {
publicKey : rsa ( ) . publicKey ( keyPair ) ,
privateKey : rsa ( ) . privateKey ( keyPair ) ,
plaintext ,
ciphertext ,
} ;
}
export async function printTestData() {
2023-02-16 12:55:24 +02:00
// eslint-disable-next-line no-console
2021-10-03 17:00:49 +02:00
console . info ( JSON . stringify ( await createTestData ( ) , null , '\t' ) ) ;
}
interface CheckTestDataOptions {
throwOnError? : boolean ;
silent? : boolean ;
}
export async function checkTestData ( data : TestData , options : CheckTestDataOptions = null ) {
options = {
throwOnError : false ,
silent : false ,
. . . options ,
} ;
// First verify that the data coming from the other app can be decrypted.
const messages : string [ ] = [ ] ;
let hasError = false ;
const keyPair = await rsa ( ) . loadKeys ( data . publicKey , data . privateKey ) ;
const decrypted = await rsa ( ) . decrypt ( data . ciphertext , keyPair ) ;
if ( decrypted !== data . plaintext ) {
messages . push ( 'RSA Tests: Data could not be decrypted' ) ;
messages . push ( 'RSA Tests: Expected:' , data . plaintext ) ;
messages . push ( 'RSA Tests: Got:' , decrypted ) ;
hasError = true ;
} else {
messages . push ( 'RSA Tests: Data could be decrypted' ) ;
}
// Then check that the public key can be used to encrypt new data, and then
// decrypt it with the private key.
{
const encrypted = await rsa ( ) . encrypt ( 'something else' , keyPair ) ;
const decrypted = await rsa ( ) . decrypt ( encrypted , keyPair ) ;
if ( decrypted !== 'something else' ) {
messages . push ( 'RSA Tests: Data could not be encrypted, then decrypted' ) ;
messages . push ( 'RSA Tests: Expected:' , 'something else' ) ;
messages . push ( 'RSA Tests: Got:' , decrypted ) ;
hasError = true ;
} else {
messages . push ( 'RSA Tests: Data could be encrypted then decrypted' ) ;
}
}
if ( hasError && options . throwOnError ) {
throw new Error ( ` Testing RSA failed: \ n ${ messages . join ( '\n' ) } ` ) ;
} else {
for ( const msg of messages ) {
if ( hasError ) {
console . warn ( msg ) ;
} else {
2023-02-16 12:55:24 +02:00
// eslint-disable-next-line no-console
2021-10-03 17:00:49 +02:00
if ( ! options . silent ) console . info ( msg ) ;
}
}
}
}
// Data generated on mobile using react-native-rsa-native
const mobileData = {
'publicKey' : '-----BEGIN PUBLIC KEY-----\nMIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAlEVSnwMpmGC+YaRw3B37BP1IBth02OFCrlZjlkn14OijnmQaOKGxhJtthvlVVEOEc50D+MMKZ1mJleER4FnD3CoGHaVZmZRa3wnuTblctF/in0mgywFJ6HlEXngUrWt2TkCnkwg4nP0IKlQ4URBxWGllVbWUgqUs5uAtV4mkrx+Ke68j+suoN8w5BF9WnYJCclDCplUOFx77llw1Z/7O8UjkgbfYKOnwMEpxlO1SVutNQNgD4BOtGn73ai0qjHKq5as8SKJb/ch+uAX95bJHlOOvBrHw718gcbnxkn6PEN3vl4/HbmHFj/V4zxG8ZF82+oTOh6m/HGdPPLpF8e98dQIDAQAB\n-----END PUBLIC KEY-----' ,
'privateKey' : '-----BEGIN RSA PRIVATE KEY-----\nMIIEowIBAAKCAQEAlEVSnwMpmGC+YaRw3B37BP1IBth02OFCrlZjlkn14OijnmQaOKGxhJtthvlVVEOEc50D+MMKZ1mJleER4FnD3CoGHaVZmZRa3wnuTblctF/in0mgywFJ6HlEXngUrWt2TkCnkwg4nP0IKlQ4URBxWGllVbWUgqUs5uAtV4mkrx+Ke68j+suoN8w5BF9WnYJCclDCplUOFx77llw1Z/7O8UjkgbfYKOnwMEpxlO1SVutNQNgD4BOtGn73ai0qjHKq5as8SKJb/ch+uAX95bJHlOOvBrHw718gcbnxkn6PEN3vl4/HbmHFj/V4zxG8ZF82+oTOh6m/HGdPPLpF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s2Y0NpMS2Ia163RPJTiBJoIJYw3KOonaDkjk+7dAeYjGlKjLTWF2yckPbYVXmu9MrREGtIpb5oii5J2lFM5oDr40iZIZ5nBiXQfm/B7/IkpJ7IXOsYeiK+UDKSWW71GFeYtICfKlowolm0jBBS+M8XJjplz2\n-----END RSA PRIVATE KEY-----' ,
'plaintext' : 'just testing' ,
'ciphertext' : 'K+saH0/1Ltnc8GFqy1gKDpIY2o4OVkNFCQGFIp1574kkjLEKIgQpgc/kOdc7EO5m\r\nAN7TKh2zGrvcB9BqMOjsQNeausQzAm+b5fYWVyRHfQ5kf6+ojUO+LMRPxKqNO58m\r\nPw5/6R7zACJn1W9cHolY8+YKAeL+guQmCoD50nEgyZc5+HMRKGZpu+Vh7y562hYu\r\n39KPCcLFzWj7yi+JtbD4bFVcgPLg8T2PXCOqj+fVkAXXdkt8PnHfgf4lbfYojZ1d\r\nge7C1hx4aVrfT7vj2saXU/RrV9MlBDtAFZDynWa+LfMmt56TWCO6yWm5KpckGU/E\r\nfEs7l00aSskIai0ghZSIvQ==' ,
} ;
// Data generated on desktop using node-rsa
const desktopData = {
'publicKey' : '-----BEGIN RSA PUBLIC KEY-----\nMIIBCgKCAQEAvLb8Lk0UBFEQ2UJsVMgKdbPYExhYqa87diBQiFBJglgNuZVi8/HX\nvpCVcH7BhdQKkA9Mh23SpNcYHR9JrzUTrn9Q21t1uj2J60+bfq1s0BA1wkS/xBPN\nrrLw2yCPpkZzNH8HcLx/MtMaOnOVfl5KqftXROzn+Vo3rrxNprd2ETLAxr+CC6SE\nTJiiP8ovUfH+TKZ3P2nkSyBy4oY24h4HA+wVnj12DspE8CMOXCyBUxlG2ki2c/sK\nDSDla3oEjB8QdpBKhIXD/Bb4MpLHfaby7O/eYjrteB8g6JU01JDsnQoomLe4FdCU\nnYK5sFNUQ89e05lMa3yxZWV3mXAVUi/mFwIDAQAB\n-----END RSA PUBLIC KEY-----' ,
'privateKey' : '-----BEGIN RSA PRIVATE KEY-----\nMIIEpA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lA0DiR8obPmzKNwBm2OZXxR6wxJAoGAC6T95SFDydqjFtkHoxTp\nOG8L0/5h2VYZyMAdop/cOonoLHZwAW2PQ8OokRgBelnh6Qe8dmfqjZdFGN8OKN87\nBddxszkjTq1IwSglxoLUC6c0IG+1ponDnrNG+UF3kTLpqzcQHb6Vgn0KkJp59ImV\n3iwmXmv10th0vjIEW99QFo8CgYEAkqF/SdwtbdlF06/fXQsIMusV7K7Bdrdee3yD\nSNtTVub0ruK1dvtEEpGIEb1QmixE5TADdCBQ2B5Pnbk7OBemb3OncFU7809f+vLx\nDwdumaZLMvSHN6qGK1kGEPziyn/y3hxyyz52/uP7hp/6skVJdSiFQ4ETdxn0mCf0\nKwSkdpkCgYBDMXv2N3eF2IElZkN+8rQYHqWck1HqTcSnlrHS0i0uPQduQa+K/O+G\nVE2S8htp4/D0Gd3EwuDBIT3vUvnx7YBuMX1fXwU0m7oKKOyQqhxfrt9t7BEh2j5r\n5pvRU7dTKVbua78w1sQQMtYnWUyukBlaF/IpHPi5hwCjmDQR1EJY4w==\n-----END RSA PRIVATE KEY-----' ,
'plaintext' : 'just testing' ,
'ciphertext' : 'PRqiQjxnQMukoYPA9XtlGcgAjwuDJd24GtJ3iO2qhh0HnbPnx3c8ZaGWJyV1ejZCwIWv509js7sCTHtXqeGkZr//Db6oOIyi77VzRwvzPxReHPefF0rX62uMh+zTmQW7KSrFeAvtnpWiDcyynUtwycgrZcQCHZoEmSSyc3cyj09HgqEoSQb0BOc8daR0aXwOpgXsB8ypf3+m23U1gZmIyl0glymTN9h1jopV9dRtw5ufcc4ve/hHKp0gbaT2OaRKOLr6AXmbDGwkF5bsvjV+v4tTkj96OUjoG9qUMQh/JYRMl7mxJriUB3Jc6WHEKRVPQYAIZODfEOy3rkHwWAcYjA==' ,
} ;
// This can be used to run integration tests directly on device. It will throw
// an error if something cannot be decrypted, or else print info messages.
2023-06-30 10:11:26 +02:00
export const runIntegrationTests = async ( silent = false ) = > {
2021-10-03 17:00:49 +02:00
const log = ( s : string ) = > {
if ( silent ) return ;
2023-02-16 12:55:24 +02:00
// eslint-disable-next-line no-console
2021-10-03 17:00:49 +02:00
console . info ( s ) ;
} ;
log ( 'RSA Tests: Running integration tests...' ) ;
log ( 'RSA Tests: Decrypting and encrypting using desktop data...' ) ;
await checkTestData ( desktopData , { silent , throwOnError : true } ) ;
log ( 'RSA Tests: Decrypting and encrypting using mobile data...' ) ;
await checkTestData ( mobileData , { silent , throwOnError : true } ) ;
log ( 'RSA Tests: Decrypting and encrypting using local data...' ) ;
const newData = await createTestData ( ) ;
await checkTestData ( newData , { silent , throwOnError : true } ) ;
} ;