Hot Door CORE  0.7.0
Adobe® Illustrator® Plug-in Library
Public Member Functions | Friends | List of all members
hdi::core::crypt::rsa::PrivateKey Class Reference

Wraps around a private RSA key's raw data for some added conveniences. More...

#include <hdicoreCrypt.h>

Public Member Functions

 PrivateKey ()
 Constructs an empty PrivateKey object. More...
 
 PrivateKey (const PrivateKey &key_)
 Constructs a PrivateKey object from another (copy constructor) More...
 
 PrivateKey (const std::string &serializedKey_)
 Constructs a PrivateKey object from a previous serialization of the key (using the serialize() method) More...
 
virtual ~PrivateKey ()
 Destructs a PrivateKey object. More...
 
PrivateKeyoperator= (const PrivateKey &rhs_)
 Assigns one PrivateKey object to another. More...
 
bool valid () const
 Gets whether the object contains a valid key. More...
 
int32_t length () const
 Gets the size/length of the key in bits. More...
 
std::string serialize () const
 Serializes the private key in a base-64 string; useful for transmitting the string to another party (not recommended for private keys!) or writing the key to disk. More...
 
bool operator== (const PrivateKey &rhs_) const
 Compares two PrivateKey objects for equality. More...
 
bool operator!= (const PrivateKey &rhs_) const
 Compares two PrivateKey objects for inequality. More...
 

Friends

bool generateKeyPair (const int32_t, PrivateKey &, PublicKey &)
 Generates a new RSA key pair of a given size/length. More...
 
bool encryptWithPublicKey (const PublicKey &, const std::string &, std::string &)
 Encrypts a string (or raw bytes stuffed into a string) with a public RSA key. More...
 
bool decryptWithPrivateKey (const PrivateKey &, const std::string &, std::string &)
 Decrypts a base-64 encoded string of cipher text (previously encrypted with the encryptWithPublicKey() function or equivalent) More...
 
bool encryptWithPrivateKey (const PrivateKey &, const std::string &, std::string &)
 Encrypts a string (or raw bytes stuffed into a string) with a private RSA key. More...
 
bool decryptWithPublicKey (const PublicKey &, const std::string &, std::string &)
 Decrypts a base-64 encoded string of cipher text (previously encrypted with the encryptWithPrivateKey() function or equivalent) More...
 

Detailed Description

Wraps around a private RSA key's raw data for some added conveniences.

Constructor & Destructor Documentation

hdi::core::crypt::rsa::PrivateKey::PrivateKey ( )

Constructs an empty PrivateKey object.

Author
GW
Date
10/2013
Note
To test if a PrivateKey object is empty/valid, call valid() on it
Empty PrivateKey objects do not relate to any actual private RSA key; they are designed to be "receivers" of some other PrivateKey object via the overloaded assignment operator. Empty PrivateKey objects are useless until such time (though it is safe to call any of their methods).
hdi::core::crypt::rsa::PrivateKey::PrivateKey ( const PrivateKey key_)

Constructs a PrivateKey object from another (copy constructor)

Author
GW
Date
10/2013
Parameters
key_PrivateKey object to copy values from
hdi::core::crypt::rsa::PrivateKey::PrivateKey ( const std::string &  serializedKey_)

Constructs a PrivateKey object from a previous serialization of the key (using the serialize() method)

Author
GW
Date
10/2013
Parameters
serializedKey_Base-64 encoded serialization of a private RSA key
virtual hdi::core::crypt::rsa::PrivateKey::~PrivateKey ( )
virtual

Destructs a PrivateKey object.

Author
GW
Date
10/2013

Member Function Documentation

int32_t hdi::core::crypt::rsa::PrivateKey::length ( ) const

Gets the size/length of the key in bits.

Author
GW
Date
10/2013
Returns
The number of bits for the private key
bool hdi::core::crypt::rsa::PrivateKey::operator!= ( const PrivateKey rhs_) const

Compares two PrivateKey objects for inequality.

Author
GW
Date
10/2013
Parameters
rhs_Righthand side of the inequality operator; PrivateKey object to compare
Returns
true if the two objects have differing key values, false otherwise
PrivateKey& hdi::core::crypt::rsa::PrivateKey::operator= ( const PrivateKey rhs_)

Assigns one PrivateKey object to another.

Author
GW
Date
10/2013
Parameters
rhs_Righthand side of the assignment operator; private key object to copy values from
Returns
A reference to the target object (lefthand side of assignment operator), but with its values updated
bool hdi::core::crypt::rsa::PrivateKey::operator== ( const PrivateKey rhs_) const

Compares two PrivateKey objects for equality.

Author
GW
Date
10/2013
Parameters
rhs_Righthand side of the equality operator; PrivateKey object to compare
Returns
true if the two objects have the same key values, false otherwise
std::string hdi::core::crypt::rsa::PrivateKey::serialize ( ) const

Serializes the private key in a base-64 string; useful for transmitting the string to another party (not recommended for private keys!) or writing the key to disk.

Author
GW
Date
10/2013
Returns
The serialized version of the key
Note
A key serialization string can be converted back into a PrivateKey object at runtime with the PrivateKey(const std::string&) constructor.
bool hdi::core::crypt::rsa::PrivateKey::valid ( ) const

Gets whether the object contains a valid key.

Author
GW
Date
10/2013
Returns
true if the key is valid/not empty, false otherwise

Friends And Related Function Documentation

bool decryptWithPrivateKey ( const PrivateKey ,
const std::string &  ,
std::string &   
)
friend

Decrypts a base-64 encoded string of cipher text (previously encrypted with the encryptWithPublicKey() function or equivalent)

Author
GW
Date
10/2013
Parameters
key_Private RSA key to use for decryption
b64Cipher_Base-64 encoded cipher text to attempt to decrypt
data__Return-by-reference for the decrypted plain text (or raw bytes stuffed into a string)
Returns
true if the data could be decrypted, false otherwise
bool decryptWithPublicKey ( const PublicKey ,
const std::string &  ,
std::string &   
)
friend

Decrypts a base-64 encoded string of cipher text (previously encrypted with the encryptWithPrivateKey() function or equivalent)

Author
GW
Date
10/2013
Parameters
key_Public RSA key to use for decryption
b64Cipher_Base-64 encoded cipher text to attempt to decrypt
data__Return-by-reference for the decrypted plain text (or raw bytes stuffed into a string)
Returns
true if the data could be decrypted, false otherwise
bool encryptWithPrivateKey ( const PrivateKey ,
const std::string &  ,
std::string &   
)
friend

Encrypts a string (or raw bytes stuffed into a string) with a private RSA key.

Author
GW
Date
10/2013
Parameters
key_Private RSA key to use for encryption
data_Plain data to be encrypted (in the form of a string or binary data put inside of a string object)
b64Cipher__Return-by-reference for the base-64 encoded cipher text
Returns
true if the data could be encrypted, false otherwise
Note
To decrypt the cipher, use the decryptWithPublicKey() function (or equivalent - this lib is built on OpenSSL and the RSA standard, so any base-64 decoder and function that understands RSA private key decryption can decrypt the cipher with the correct key).
Typically, sensitive data is not encrypted with a private key because anyone with the public key can decrypt it. However, "signing" (encrypting) some token data with a private key is a common practice to prove to a holder of the public key that the true owner of the private key e.g. received a message and is responding to it (as only the holder of the private key could do this).
bool encryptWithPublicKey ( const PublicKey ,
const std::string &  ,
std::string &   
)
friend

Encrypts a string (or raw bytes stuffed into a string) with a public RSA key.

Author
GW
Date
10/2013
Parameters
key_Public RSA key to use for encryption
data_Plain data to be encrypted (in the form of a string or binary data put inside of a string object)
b64Cipher__Return-by-reference for the base-64 encoded cipher text
Returns
true if the data could be encrypted, false otherwise
Note
To decrypt the cipher, use the decryptWithPrivateKey() function (or equivalent - this lib is built on OpenSSL and the RSA standard, so any base-64 decoder and function that understands RSA private key decryption can decrypt the cipher with the correct key).
bool generateKeyPair ( const int32_t  ,
PrivateKey ,
PublicKey  
)
friend

Generates a new RSA key pair of a given size/length.

Author
GW
Date
10/2013
Parameters
size_Size/length of the key in bits (must be n*1024; not recommended for n>16)
privKey__Return-by-reference for the new private key
pubKey__Return-by-reference for the new public key
Returns
true if the key pair could be generated, false otherwise
Note
A minimum key size of 2048 bits is recommended.