react-native-rsa-native
Advanced tools
Comparing version 1.0.24 to 1.0.25
{ | ||
"name": "react-native-rsa-native", | ||
"version": "1.0.24", | ||
"version": "1.0.25", | ||
"description": "A native implementation of RSA key generation and encryption/decryption.", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
235
README.md
@@ -14,8 +14,8 @@ # react-native-rsa-native | ||
Features: | ||
Generation, | ||
Encryption, | ||
Decryption, | ||
Sign, | ||
Verify, | ||
Features: | ||
Generation, | ||
Encryption, | ||
Decryption, | ||
Sign, | ||
Verify, | ||
Keychain support | ||
@@ -39,78 +39,181 @@ | ||
## Usage | ||
## Example Usage | ||
``` | ||
These basic examples show a typical use case using both promise chains | ||
and async/await. | ||
See [the full API documentation below](#documentation) | ||
for more detail on the methods available. | ||
import {RSA, RSAKeychain} from 'react-native-rsa-native'; | ||
### Encrypt a message | ||
Encrypt a message and subsequently decrypt it, | ||
using the RSA class in a promise chain structure. | ||
```js | ||
import { RSA } from 'react-native-rsa-native'; | ||
let message = "my secret message"; | ||
RSA.generateKeys(4096) // set key size | ||
.then(keys => { | ||
console.log('4096 private:', keys.private) // the private key | ||
console.log('4096 public:', keys.public) // the public key | ||
}) | ||
RSA.generate() | ||
.then(keys => { | ||
console.log(keys.private) // the private key | ||
console.log(keys.public) // the public key | ||
RSA.encrypt('1234', keys.public) | ||
.then(encodedMessage => { | ||
.then(keys => { | ||
console.log('4096 private:', keys.private); // the private key | ||
console.log('4096 public:', keys.public); // the public key | ||
RSA.encrypt(message, keys.public) | ||
.then(encodedMessage => { | ||
console.log(`the encoded message is ${encodedMessage}`); | ||
RSA.decrypt(encodedMessage, keys.private) | ||
.then(message => { | ||
console.log(message); | ||
}) | ||
}) | ||
.then(decryptedMessage => { | ||
console.log(`The original message was ${decryptedMessage}`); | ||
}); | ||
}); | ||
}); | ||
``` | ||
RSA.sign(secret, keys.private) | ||
.then(signature => { | ||
console.log(signature); | ||
### Sign a message | ||
RSA.verify(signature, secret, keys.public) | ||
.then(valid => { | ||
console.log(valid); | ||
}) | ||
}) | ||
}) | ||
Sign a message and subsequently verify it, | ||
using the RSAKeychain class in an async/await structure. | ||
// Example utilizing the keychain for private key secure storage | ||
```typescript | ||
import { RSAKeychain } from 'react-native-rsa-native'; | ||
let keyTag = 'com.domain.mykey'; | ||
let secret = "secret message"; | ||
async main() { | ||
let keyTag = 'com.domain.mykey'; | ||
let message = "message to be verified"; | ||
RSAKeychain.generate(keyTag) | ||
.then(keys => { | ||
console.log(keys.public); | ||
console.log(secret); | ||
let publicKey = await generateKeyPair(keyTag); | ||
// Share the generated public key with third parties as desired. | ||
return RSAKeychain.encrypt(secret, keyTag) | ||
.then(encodedMessage => { | ||
console.log(encodedMessage); | ||
let messageSignature = await RSAKeychain.sign(message, keyTag); | ||
RSAKeychain.decrypt(encodedMessage, keyTag) | ||
.then(message => { | ||
console.log(message); | ||
}) | ||
}) | ||
}) | ||
.then(() => { | ||
return RSAKeychain.sign(secret, keyTag) | ||
.then(signature => { | ||
console.log('signature', signature); | ||
if (await RSAKeychain.verify(messageSignature, message, keyTag)) { | ||
// The signature matches: trust this message. | ||
} else { | ||
// The signature does not match. | ||
} | ||
RSAKeychain.verify(signature, secret, keyTag) | ||
.then(valid => { | ||
console.log('verified', valid); | ||
}) | ||
}) | ||
}) | ||
.then(() => { | ||
RSAKeychain.deletePrivateKey(keyTag) | ||
.then( success => { | ||
console.log('delete success', success) | ||
}) | ||
}); | ||
await RSAKeychain.deletePrivateKey(keyTag); | ||
} | ||
async generateKeyPair(keyTag : string) { | ||
let keys = await RSAKeychain.generate(keyTag); | ||
return keys.public; | ||
} | ||
``` | ||
Check out example App.js for a full example | ||
## Documentation | ||
### RSA Class | ||
A class that performs RSA cryptographic primitives | ||
in a simple and straightforward manner. | ||
If you would prefer to use the underlying operating system's built-in | ||
security keychain, use [the RSAKeychain Class](#rsakeychain-class) instead. | ||
#### generateKeys | ||
`static generateKeys(keySize : number) : Promise<KeyPair>` | ||
Generate a public/private key pair of the given key size. | ||
#### generate | ||
`static generate() : Promise<KeyPair>` | ||
Equivalent to `generateKeys(2048)` | ||
#### encrypt | ||
`static encrypt(message : string, publicKey : string) : Promise<string>` | ||
Encrypt a given message with the provided public key, so it is decryptable with the matching private key. | ||
#### decrypt | ||
`static decrypt(encodedMessage : string, privateKey : string) : Promise<string>` | ||
Decrypt a given encrypted message using the private key. | ||
#### sign | ||
`static sign(message: string, privateKey : string) : Promise<string>` | ||
Sign a given message with the private key, so that any user with the message, the returned signature, and the matching public key can verify it was signed under this key. | ||
#### verify | ||
`static verify(signature : string, message : string, publicKey : string) : Promise<boolean>` | ||
Verify whether or not a provided signature was produced by signing the given message with the private key paired to the provided public key. | ||
### RSAKeychain Class | ||
Like [the RSA Class](#rsa-class), | ||
but when its methods are called, instead of directly accessing the private key, | ||
the private key is stored in the underlying operating system's keychain | ||
(see documentation | ||
[for iOS](https://developer.apple.com/documentation/security/keychain_services) and | ||
[for Android](https://developer.android.com/reference/android/security/KeyChain)) | ||
using a tag which the app can use to access it. | ||
Methods then take this tag instead of the private key. | ||
#### generateKeys | ||
`static generateKeys(keySize : number, keyTag : string) : Promise<PublicKey>` | ||
Generate a public/private key pair of the given key size, | ||
and store the private key in the operating system keychain. | ||
#### generate | ||
`static generate(keyTag : string) : Promise<KeyPair>` | ||
Equivalent to `generateKeys(2048, keyTag)` | ||
#### encrypt | ||
`static encrypt(message : string, keyTag : string) : Promise<string>` | ||
Retrieve the public key associated with the key tag, | ||
and encrypt a given message with that key, | ||
so it is decryptable with the matching private key. | ||
#### decrypt | ||
`static decrypt(encodedMessage : string, keyTag : string) : Promise<string>` | ||
Decrypt a given encrypted message using the private key | ||
associated with the given key tag. | ||
#### sign | ||
`static sign(message: string, keyTag : string) : Promise<string>` | ||
Sign a given message with the private key associated with the given key tag, | ||
so that any user with | ||
the message, the returned signature, and the matching public key | ||
can verify it was signed under this key. | ||
#### verify | ||
`static verify(signature : string, message : string, keyTag : string) : Promise<boolean>` | ||
Verify whether or not a provided signature was produced by signing the given message with private key associated with the given key tag. | ||
#### deletePrivateKey | ||
`static deletePrivateKey(keyTag : string) : Promise<boolean>` | ||
Delete the private key from the operating system's keychain. | ||
Returns true if the key was removed successfully. | ||
### KeyPair Type | ||
Note: The `KeyPair` type does not strictly exist. | ||
Documentation provided here for convenience of understanding the return types | ||
of other methods. | ||
Property | Description | ||
--|-- | ||
`private : string` | The RSA private key. | ||
`public : string` | The RSA public key. | ||
### PublicKey Type | ||
Note: The `PublicKey` type does not strictly exist. | ||
Documentation provided here for convenience of understanding the return types | ||
of other methods. | ||
Property | Description | ||
--|-- | ||
`public : string` | The RSA public key. | ||
## Credit | ||
@@ -124,2 +227,2 @@ | ||
ETH: 0xDc2F8D78098749EB3ECdF79Fe32Efda86fEEFc3c | ||
ETH: 0xDc2F8D78098749EB3ECdF79Fe32Efda86fEEFc3c |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
137013
24
226