Wraps around a private RSA key's raw data for some added conveniences.
More...
#include <hdicoreCrypt.h>
|
bool | generateKeyPair (const int32_t, PrivateKey &, PublicKey &) |
| Generates a new RSA key pair of a given size/length.
|
|
bool | encryptWithPublicKey (const PublicKey &, const std::string &, std::string &) |
| Encrypts a string (or raw bytes stuffed into a string) with a public RSA key.
|
|
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)
|
|
bool | encryptWithPrivateKey (const PrivateKey &, const std::string &, std::string &) |
| Encrypts a string (or raw bytes stuffed into a string) with a private RSA key.
|
|
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)
|
|
Wraps around a private RSA key's raw data for some added conveniences.
◆ PrivateKey() [1/3]
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).
◆ PrivateKey() [2/3]
hdi::core::crypt::rsa::PrivateKey::PrivateKey |
( |
const PrivateKey & |
key_ | ) |
|
Constructs a PrivateKey object from another (copy constructor)
- Author
- GW
- Date
- 10/2013
- Parameters
-
◆ PrivateKey() [3/3]
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 |
◆ ~PrivateKey()
virtual hdi::core::crypt::rsa::PrivateKey::~PrivateKey |
( |
| ) |
|
|
virtual |
Destructs a PrivateKey object.
- Author
- GW
- Date
- 10/2013
◆ length()
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
◆ operator!=()
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
◆ operator=()
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
◆ operator==()
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
◆ serialize()
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.
◆ valid()
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
◆ decryptWithPrivateKey
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
◆ decryptWithPublicKey
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
◆ encryptWithPrivateKey
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).
◆ encryptWithPublicKey
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).
◆ generateKeyPair
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.