master
IoTgod 5 years ago
parent b0bc94d129
commit 3e0dded126
  1. 114
      src/ovo.cpp
  2. 20
      src/ovo.h

@ -191,8 +191,8 @@ void ovo::file::get_files_info(const string path, const string format, const int
* @data Oct 19 2014
*/
/* Define the static member of md5. */
const byte ovo::math::md5::PADDING[64] = { 0x80 };
const char ovo::math::md5::HEX_NUMBERS[16] = {
const byte ovo::math::MD5::PADDING[64] = { 0x80 };
const char ovo::math::MD5::HEX_NUMBERS[16] = {
'0', '1', '2', '3',
'4', '5', '6', '7',
'8', '9', 'a', 'b',
@ -205,7 +205,7 @@ const char ovo::math::md5::HEX_NUMBERS[16] = {
* @param {message} the message will be transformed.
*
*/
ovo::math::md5::md5(const string& message)
ovo::math::MD5::MD5(const string& message)
{
finished = false;
/* Reset number of bits. */
@ -226,7 +226,7 @@ ovo::math::md5::md5(const string& message)
* @return the message-digest.
*
*/
const byte* ovo::math::md5::getDigest()
const byte* ovo::math::MD5::getDigest()
{
if (!finished) {
finished = true;
@ -270,7 +270,7 @@ const byte* ovo::math::md5::getDigest()
* @param {len} the number btye of message.
*
*/
void ovo::math::md5::init(const byte* input, size_t len)
void ovo::math::MD5::init(const byte* input, size_t len)
{
bit32 i, index, partLen;
@ -310,7 +310,7 @@ void ovo::math::md5::init(const byte* input, size_t len)
*
* @param {block} the message block.
*/
void ovo::math::md5::transform(const byte block[64])
void ovo::math::MD5::transform(const byte block[64])
{
bit32 a = state[0], b = state[1], c = state[2], d = state[3], x[16];
@ -404,7 +404,7 @@ void ovo::math::md5::transform(const byte block[64])
* @param {length} the length of input.
*
*/
void ovo::math::md5::encode(const bit32* input, byte* output, size_t length)
void ovo::math::MD5::encode(const bit32* input, byte* output, size_t length)
{
for (size_t i = 0, j = 0; j < length; ++i, j += 4) {
@ -425,7 +425,7 @@ void ovo::math::md5::encode(const bit32* input, byte* output, size_t length)
* @param {length} the length of input.
*
*/
void ovo::math::md5::decode(const byte* input, bit32* output, size_t length)
void ovo::math::MD5::decode(const byte* input, bit32* output, size_t length)
{
for (size_t i = 0, j = 0; j < length; ++i, j += 4) {
@ -441,7 +441,7 @@ void ovo::math::md5::decode(const byte* input, bit32* output, size_t length)
* @return the hex string of digest.
*
*/
string ovo::math::md5::toStr(const int length)
string ovo::math::MD5::toStr(const int length)
{
const byte* digest_ = getDigest();
string str;
@ -458,4 +458,100 @@ string ovo::math::md5::toStr(const int length)
}
/*** base64 ***/
/**
*
* @From file base64.h
* @author ReneNyffenegger
* @mail rene.nyffenegger@adp-gmbh.ch
* @github https://github.com/ReneNyffenegger
*
*/
const string ovo::math::base64_chars =
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"abcdefghijklmnopqrstuvwxyz"
"0123456789+/";
string ovo::math::base64_encode(const string fromStr)
{
char const* bytes_to_encode = fromStr.c_str();
unsigned int in_len = fromStr.length();
string ret;
int i = 0;
int j = 0;
unsigned char char_array_3[3];
unsigned char char_array_4[4];
while(in_len--) {
char_array_3[i++] = *(bytes_to_encode++);
if(i == 3) {
char_array_4[0] = (char_array_3[0] & 0xfc) >> 2;
char_array_4[1] = ((char_array_3[0] & 0x03) << 4) + ((char_array_3[1] & 0xf0) >> 4);
char_array_4[2] = ((char_array_3[1] & 0x0f) << 2) + ((char_array_3[2] & 0xc0) >> 6);
char_array_4[3] = char_array_3[2] & 0x3f;
for(i = 0; (i <4) ; i++)
ret += base64_chars[char_array_4[i]];
i = 0;
}
}
if(i) {
for(j = i; j < 3; j++)
char_array_3[j] = '\0';
char_array_4[0] = ( char_array_3[0] & 0xfc) >> 2;
char_array_4[1] = ((char_array_3[0] & 0x03) << 4) + ((char_array_3[1] & 0xf0) >> 4);
char_array_4[2] = ((char_array_3[1] & 0x0f) << 2) + ((char_array_3[2] & 0xc0) >> 6);
for (j = 0; (j < i + 1); j++)
ret += base64_chars[char_array_4[j]];
while((i++ < 3))
ret += '=';
}
return ret;
}
string ovo::math::base64_decode(string const& encoded_string)
{
int in_len = encoded_string.size();
int i = 0;
int j = 0;
int in_ = 0;
unsigned char char_array_4[4], char_array_3[3];
string ret;
while(in_len-- && ( encoded_string[in_] != '=') && is_base64(encoded_string[in_])) {
char_array_4[i++] = encoded_string[in_]; in_++;
if(i ==4) {
for (i = 0; i <4; i++)
char_array_4[i] = base64_chars.find(char_array_4[i]);
char_array_3[0] = ( char_array_4[0] << 2) + ((char_array_4[1] & 0x30) >> 4);
char_array_3[1] = ((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2);
char_array_3[2] = ((char_array_4[2] & 0x3) << 6) + char_array_4[3];
for(i = 0; (i < 3); i++)
ret += char_array_3[i];
i = 0;
}
}
if(i) {
for(j = 0; j < i; j++)
char_array_4[j] = base64_chars.find(char_array_4[j]);
char_array_3[0] = (char_array_4[0] << 2) + ((char_array_4[1] & 0x30) >> 4);
char_array_3[1] = ((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2);
for (j = 0; (j < i - 1); j++) ret += char_array_3[j];
}
return ret;
}

@ -83,6 +83,7 @@
(a) += (b); \
}
/* Define of btye.*/
typedef unsigned char byte;
/* Define of byte. */
@ -150,16 +151,22 @@ namespace ovo{
class math {
public:
inline string MD5 (const string message) const {
md5 m(message);
/*** md5 ***/
inline string md5 (const string message) const {
MD5 m(message);
return m.toStr();
}
class md5 {
/*** base64 ***/
string base64_encode(const string fromStr);
string base64_decode(string const& s);
class MD5 {
public:
/*** md5 ***/
/* Construct a md5 object with a string. */
md5(const string& message);
MD5(const string& message);
/* Generate md5 digest. */
const byte* getDigest();
/* Convert digest to string value */
@ -193,6 +200,11 @@ namespace ovo{
};
private:
/*** base64 ***/
static const string base64_chars;
static inline bool is_base64(unsigned char c) {
return (isalnum(c) || (c == '+') || (c == '/'));
}
};
}

Loading…
Cancel
Save