Ripe
256-bit security tool
Classes | Static Public Member Functions | Static Public Attributes | List of all members
Ripe Class Reference

The Ripe class is core of Ripe library and contains all the required cryptography API supported by Ripe. More...

#include <Ripe.h>

Classes

struct  KeyPair
 RSA Key pair. More...
 

Static Public Member Functions

static std::string encryptAES (const std::string &data, const RipeByte *key, std::size_t keySize, std::vector< RipeByte > &iv)
 Encrypts data of length with symmetric key of size = keySize with specified initialization vector.
 
static std::string decryptAES (const std::string &data, const RipeByte *key, std::size_t keySize, std::vector< RipeByte > &iv)
 Decrypts data of specified length with specified key and initialization vector.
 
static std::string generateNewKey (int length)
 Generate random AES key. More...
 
static std::string encryptAES (std::string &data, const std::string &hexKey, const std::string &clientId, const std::string &outputFile, const std::string &iv="")
 encryptAES Encrypts data with provided symmetric key More...
 
static std::string encryptAES (const std::string &buffer, const std::string &hexKey, std::vector< RipeByte > &iv)
 Helper function that takes hex key. More...
 
static std::string decryptAES (std::string &data, const std::string &hexKey, std::string &iv, bool isBase64=false, bool isHex=false)
 decryptAES Decrypts data using specified symmetric key. More...
 
static std::size_t expectedAESCipherLength (std::size_t plainDataSize)
 Exceptect size of AES cipher when plainDataSize size data is encrypted.
 
static bool normalizeHex (std::string &iv)
 normalizeIV If IV with no space is provided e.g, More...
 
static std::string encryptRSA (const std::string &data, const std::string &publicKeyPEM)
 Encrypts data of length = dataLength using RSA key and puts it in destination. More...
 
static std::string decryptRSA (const std::string &data, const std::string &privateKeyPEM, const std::string &secret="")
 Decrypts encryptedData of length dataLength with RSA key and puts result in destination. More...
 
static bool verifyRSA (const std::string &data, const std::string &signatureHex, const std::string &publicKeyPEM)
 Verifies the data is signed by associated private key. More...
 
static std::string signRSA (const std::string &data, const std::string &privateKeyPEM, const std::string &secret="")
 Signs the data with private key. More...
 
static KeyPair generateRSAKeyPair (unsigned int length=DEFAULT_RSA_LENGTH, const std::string &secret="")
 Generate key pair and returns KeyPair. More...
 
static unsigned int maxRSABlockSize (std::size_t keySize)
 Maximum size of RSA block with specified key size. More...
 
static unsigned int minRSAKeySize (std::size_t dataSize)
 Minimum size of RSA key to encrypt data of dataSize size.
 
static std::string encryptRSA (std::string &data, const std::string &key, const std::string &outputFile, bool isRaw=false)
 encryptRSA Encrypts using RSA key More...
 
static std::string decryptRSA (std::string &data, const std::string &key, bool isBase64, bool isHex=false, const std::string &secret="")
 decryptRSA Decrypts using RSA key More...
 
static bool writeRSAKeyPair (const std::string &publicFile, const std::string &privateFile, int length=DEFAULT_RSA_LENGTH, const std::string &secret="")
 writeRSAKeyPair Writes RSA key pair to public and private file paths. More...
 
static std::string generateRSAKeyPairBase64 (int length=DEFAULT_RSA_LENGTH, const std::string &secret="")
 generateRSAKeyPair Generates RSA key pair and returns colon seperated base64 where first part is private key and second part is public key.
 
static std::string base64Decode (const std::string &base64Encoded)
 Decodes encoded base64.
 
static std::string base64Encode (const std::string &binaryData)
 Encodes input of length to base64 encoding.
 
static std::size_t expectedBase64Length (std::size_t n)
 expectedBase64Length Returns expected base64 length More...
 
static bool isBase64 (const std::string &data)
 Finds whether data is base64 encoded. This is done by finding non-base64 character. So it is not necessary a valid base64 encoding.
 
static bool compressFile (const std::string &gzFilename, const std::string &inputFile)
 Compress input file (path) and create new file. More...
 
static std::string compressString (const std::string &str)
 Compresses string using zlib (inflate) More...
 
static std::string decompressString (const std::string &str)
 Decompresses string using zlib (deflate) More...
 
static std::string sha256Hash (const std::string &)
 Generate SHA-256 hash of given string.
 
static std::string sha512Hash (const std::string &)
 Generate SHA-512 hash of given string.
 
static std::string prepareData (const std::string &data, const std::string &hexKey, const char *clientId="", const std::string &ivec="")
 prepareData Helper method to encrypt data with symmetric key and convert it in to tranferable data. More...
 
static std::size_t expectedDataSize (std::size_t plainDataSize, std::size_t clientIdSize=16)
 Calculates expected data size. Assumed IV size = 32. More...
 
static std::string stringToHex (const std::string &raw)
 Helper function to convert string to hexdecimal e.g, khn = 6b686e.
 
static std::string hexToString (const std::string &hex)
 Helper function to convert hexadecimal input to raw data.
 
static std::string vecToString (const std::vector< RipeByte > &iv)
 Converts vector of RipeByte to raw string.
 
static std::vector< RipeByte > RipeByteToVec (const RipeByte *iv)
 ivToVector Converts plain (unsigned char*) IV to std::vector<RipeByte>
 
static std::string version ()
 version Version of Ripe library
 

Static Public Attributes

static const std::string PACKET_DELIMITER
 Constant for end of data delimiter.
 
static const std::size_t PACKET_DELIMITER_SIZE
 Size of PACKET_DELIMITER. More...
 
static const char DATA_DELIMITER
 Data delimiter for prepared data. More...
 
static const int DEFAULT_RSA_LENGTH
 Constant value default rsa length.
 
static const int AES_BLOCK_SIZE
 Constant value for AES block size.
 
static const std::string BASE64_CHARS
 Possible base64 characters.
 
static const std::string PRIVATE_RSA_ALGORITHM
 Algorithm for private rsa key.
 
static const int ZLIB_BUFFER_SIZE
 Buffer size for zlib.
 

Detailed Description

The Ripe class is core of Ripe library and contains all the required cryptography API supported by Ripe.

Member Function Documentation

◆ compressFile()

static bool Ripe::compressFile ( const std::string &  gzFilename,
const std::string &  inputFile 
)
static

Compress input file (path) and create new file.

Parameters
gzFilenameOutput file path
inputFileInput file path
Returns
True if successful, otherwise false

◆ compressString()

static std::string Ripe::compressString ( const std::string &  str)
static

Compresses string using zlib (inflate)

Parameters
strInput plain text
Returns
Raw output (binary)

◆ decompressString()

static std::string Ripe::decompressString ( const std::string &  str)
static

Decompresses string using zlib (deflate)

Parameters
strRaw input
Returns
Plain output

◆ decryptAES()

static std::string Ripe::decryptAES ( std::string &  data,
const std::string &  hexKey,
std::string &  iv,
bool  isBase64 = false,
bool  isHex = false 
)
static

decryptAES Decrypts data using specified symmetric key.

Parameters
isBase64If true, first base64 decoding is done on data and then decryption is processed
isHexIf true, hex string is decoded. If base64 is true, base64 decoding is done before hex decoding

◆ decryptRSA() [1/2]

static std::string Ripe::decryptRSA ( const std::string &  data,
const std::string &  privateKeyPEM,
const std::string &  secret = "" 
)
static

Decrypts encryptedData of length dataLength with RSA key and puts result in destination.

Ripe uses PKCS #1 v1.5 padding scheme

Returns
The size of the recovered plaintext. On error -1 is returned. use printLastError(const char* name) to see the error details

◆ decryptRSA() [2/2]

static std::string Ripe::decryptRSA ( std::string &  data,
const std::string &  key,
bool  isBase64,
bool  isHex = false,
const std::string &  secret = "" 
)
static

decryptRSA Decrypts using RSA key

Parameters
isHexIf true, hex string is decoded. If base64 is true, base64 decoding is done before hex decoding
isBase64If true, first base64 decoding is done on data and then decryption is processed

◆ encryptAES() [1/2]

static std::string Ripe::encryptAES ( std::string &  data,
const std::string &  hexKey,
const std::string &  clientId,
const std::string &  outputFile,
const std::string &  iv = "" 
)
static

encryptAES Encrypts data with provided symmetric key

Parameters
outputFileOptional, if provided instead of printing it to console data is saved to file and IV is printed on console

◆ encryptAES() [2/2]

static std::string Ripe::encryptAES ( const std::string &  buffer,
const std::string &  hexKey,
std::vector< RipeByte > &  iv 
)
inlinestatic

Helper function that takes hex key.

See also
encryptAES(std::string& data, const std::string& hexKey, const std::string& clientId, const std::string& outputFile)

◆ encryptRSA() [1/2]

static std::string Ripe::encryptRSA ( const std::string &  data,
const std::string &  publicKeyPEM 
)
static

Encrypts data of length = dataLength using RSA key and puts it in destination.

Ripe uses PKCS #1 v1.5 padding scheme

Returns
The size of the encrypted data. On error -1 is returned. use printLastError(const char*) to see the error details

◆ encryptRSA() [2/2]

static std::string Ripe::encryptRSA ( std::string &  data,
const std::string &  key,
const std::string &  outputFile,
bool  isRaw = false 
)
static

encryptRSA Encrypts using RSA key

Parameters
outputFileOptional, if provided instead of printing it to console data is saved to file
isRawOutputs raw data

◆ expectedBase64Length()

static std::size_t Ripe::expectedBase64Length ( std::size_t  n)
inlinestatic

expectedBase64Length Returns expected base64 length

Parameters
nLength of input (plain data)

◆ expectedDataSize()

static std::size_t Ripe::expectedDataSize ( std::size_t  plainDataSize,
std::size_t  clientIdSize = 16 
)
static

Calculates expected data size. Assumed IV size = 32.

See also
prepareData(const char*, const std::string&, const char*)

◆ generateNewKey()

static std::string Ripe::generateNewKey ( int  length)
static

Generate random AES key.

Parameters
lengthLength of key, must be 16, 24 or 32
Returns
Hexadecimal value of key

◆ generateRSAKeyPair()

static KeyPair Ripe::generateRSAKeyPair ( unsigned int  length = DEFAULT_RSA_LENGTH,
const std::string &  secret = "" 
)
static

Generate key pair and returns KeyPair.

Parameters
lengthLength of the key (2048 for 256-bit key, ...)
secretPassword for private RSA key (if any)
See also
KeyPair

◆ maxRSABlockSize()

static unsigned int Ripe::maxRSABlockSize ( std::size_t  keySize)
inlinestatic

Maximum size of RSA block with specified key size.

Parameters
keySize2048 for 256-bit key, ...

◆ normalizeHex()

static bool Ripe::normalizeHex ( std::string &  iv)
static

normalizeIV If IV with no space is provided e.g,

67e56fee50e22a8c2ba05c0fb2932bfa:

normalized IV is

67 e5 6f ee 50 e2 2a 8c 2b a0 5c 0f b2 93 2b fa:

◆ prepareData()

static std::string Ripe::prepareData ( const std::string &  data,
const std::string &  hexKey,
const char *  clientId = "",
const std::string &  ivec = "" 
)
static

prepareData Helper method to encrypt data with symmetric key and convert it in to tranferable data.

Parameters
clientIdExtra text in between representing client ID (leave empty if you don't need it)
ivecInit vector, if empty, random is generated
Returns
Base64 format of encrypted data with format:
[LENGTH]:[IV]:[[Client_ID]:]:[Base64 Data]

◆ signRSA()

static std::string Ripe::signRSA ( const std::string &  data,
const std::string &  privateKeyPEM,
const std::string &  secret = "" 
)
static

Signs the data with private key.

Parameters
dataThe data to sign
privateKeyPEMprivate key to sign the data with
secretPrivate key secret
Returns
Hex format signature

◆ verifyRSA()

static bool Ripe::verifyRSA ( const std::string &  data,
const std::string &  signatureHex,
const std::string &  publicKeyPEM 
)
static

Verifies the data is signed by associated private key.

Parameters
dataThe data to verify
signatureHexSignature in hex format
publicKeyPEMPublic key to verify the data from

◆ writeRSAKeyPair()

static bool Ripe::writeRSAKeyPair ( const std::string &  publicFile,
const std::string &  privateFile,
int  length = DEFAULT_RSA_LENGTH,
const std::string &  secret = "" 
)
static

writeRSAKeyPair Writes RSA key pair to public and private file paths.

Parameters
publicFileOutput path for public file. It must be wriable.
privateFileOutput path for private file. It must be writable.
lengthLength of the key (2048 for 256-bit key, ...)
secretPassword for private RSA key (if any)

Member Data Documentation

◆ DATA_DELIMITER

const char Ripe::DATA_DELIMITER
static

Data delimiter for prepared data.

See also
prepareData(const char*, const std::string&, const char*)

◆ PACKET_DELIMITER_SIZE

const std::size_t Ripe::PACKET_DELIMITER_SIZE
static

Size of PACKET_DELIMITER.

See also
PACKET_DELIMITER

The documentation for this class was generated from the following file: