1.2.0 • Published 3 years ago

nativescript-rsa v1.2.0

Weekly downloads
3
License
MIT
Repository
-
Last release
3 years ago

Nativescript RSA

Simplifying RSA key generation, signing and verifying using native API:s.

Installation

Install using nativescript-cli:

tns plugin add nativescript-rsa

Usage

import { Rsa, RsaHashAlgorithm } from 'nativescript-rsa';

var rsa = new Rsa();
var key = rsa.generateKey("org.nativescript.my-app.rsa-key", 2048);
var message = "Hello world";
var signature = rsa.sign(message, key, RsaHashAlgorithm.SHA256);
console.log('signature is ' + signature);
var isValid = rsa.verify(signature, message, key, RsaHashAlgorithm.SHA256);
console.log('signature is valid = ' + isValid);

API

The API is relatively straight forward. The Rsa class is normally the only class you need to use directly.

Class: Rsa

The main class for generating keys, signing and verifying. It contains the following methods:

importPublicKey

importPublicKey(tag: string, key: string): RsaKey;

Imports a public key in PEM format into the keychain and returns a RsaKey instance.

ArgumentTypeDescription
tagstringThe tag or alias to associate with this key when storing in the keychain
keystringPEM encoded public key in BASE64. May include the PEM headers (-----BEGIN PUBLIC KEY-----, -----END PUBLIC KEY-----)

loadKey

loadKey(tag: string): RsaKey;

Loads the key with the specified tag from the keychain. Returns a RsaKey instance if found, null otherwise.

removeKeyFromKeychain

removeKeyFromKeychain(tag: string): void;

Removes key with the specified tag from the keychain.

generateKey

generateKey(tag: string, keySize: number, permanent?: boolean): RsaKey;

Generate a new Private/Public key pair with the specified key size.

ArgumentTypeDescription
tagstringThe tag or alias to associate with this key when storing in the keychain
keySizenumberKey size in bits
permanentboolean(optional) Make this key persistent in the keychain. Non-Persistent keys remain available until you do not use them any more

sign

sign(data: string, key: RsaKey, alg: RsaHashAlgorithm): string;

Create a signature of specified data with a private key. Returns the signature as a Base64-string.

ArgumentTypeDescription
datastringThe data to be signed. Must be in UTF-8 encoding.
keyRsaKeyPrivate key to sign with.
algRsaHashAlgorithmThe algorithm to use. See RsaHashAlgorithm below for possible values

verify

verify(signature: string, data: string, key: RsaKey, alg: RsaHashAlgorithm): boolean;

Verifies a signature using a public key.

ArgumentTypeDescription
signaturestringThe signature to verify. Must be a base64-string.
datastringThe data to be signed. Must be in UTF-8 encoding.
keyRsaKeyPublic key to verify with. If a private key is specified, the public key will be extracted from it.
algRsaHashAlgorithmThe algorithm to use. See RsaHashAlgorithm below for possible values

Class: RsaKey

This class is a wrapper around the platform specific native keys.

Internally uses SecKeyRef for iOS and java.security.KeyPair for Android.

constructor

constructor(data: SecKeyRef | java.security.KeyPair);

Create a RsaKey instance using a native key as data.

valueOf

valueOf(): SecKeyRef | java.security.KeyPair;

Returns the native key that this RsaKey instance wraps.

getPublicKey

getPublicKey(): string;

Returns the public key in PEM-format.

Example:

-----BEGIN PUBLIC KEY-----
MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBALj+sLdjL7fllvqQs+q4cgwtbAK2bgtx
pVRR/GQIpQSdqAFQnGVY5bVTMGozSNznQ+QhqvnZhUOO3G88SFJiSHUCAwEAAQ==
-----END PUBLIC KEY-----

Enum: RsaHashAlgorithm

Has one of the following values:

  SHA1
  SHA224
  SHA256
  SHA384
  SHA512

License

The MIT License (MIT)