|
|
|
@ -13,77 +13,13 @@ |
|
|
|
|
#define _OVO_H |
|
|
|
|
|
|
|
|
|
#include <iostream> |
|
|
|
|
#include <fstream> |
|
|
|
|
#include <sstream> |
|
|
|
|
#include <string> |
|
|
|
|
#include <vector> |
|
|
|
|
#include <cstring> |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* Parameters of md5. */ |
|
|
|
|
#define s11 7 |
|
|
|
|
#define s12 12 |
|
|
|
|
#define s13 17 |
|
|
|
|
#define s14 22 |
|
|
|
|
#define s21 5 |
|
|
|
|
#define s22 9 |
|
|
|
|
#define s23 14 |
|
|
|
|
#define s24 20 |
|
|
|
|
#define s31 4 |
|
|
|
|
#define s32 11 |
|
|
|
|
#define s33 16 |
|
|
|
|
#define s34 23 |
|
|
|
|
#define s41 6 |
|
|
|
|
#define s42 10 |
|
|
|
|
#define s43 15 |
|
|
|
|
#define s44 21 |
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @Basic ovo::math::md5 functions. |
|
|
|
|
* |
|
|
|
|
* @param there bit32. |
|
|
|
|
* |
|
|
|
|
* @return one bit32. |
|
|
|
|
*/ |
|
|
|
|
#define F(x, y, z) (((x) & (y)) | ((~x) & (z))) |
|
|
|
|
#define G(x, y, z) (((x) & (z)) | ((y) & (~z))) |
|
|
|
|
#define H(x, y, z) ((x) ^ (y) ^ (z)) |
|
|
|
|
#define I(x, y, z) ((y) ^ ((x) | (~z))) |
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @Rotate Left. |
|
|
|
|
* |
|
|
|
|
* @param {num} the raw number. |
|
|
|
|
* |
|
|
|
|
* @param {n} rotate left n. |
|
|
|
|
* |
|
|
|
|
* @return the number after rotated left. |
|
|
|
|
*/ |
|
|
|
|
#define ROTATELEFT(num, n) (((num) << (n)) | ((num) >> (32-(n)))) |
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @Transformations for rounds 1, 2, 3, and 4. |
|
|
|
|
*/ |
|
|
|
|
#define FF(a, b, c, d, x, s, ac) { \ |
|
|
|
|
(a) += F ((b), (c), (d)) + (x) + ac; \
|
|
|
|
|
(a) = ROTATELEFT ((a), (s)); \
|
|
|
|
|
(a) += (b); \
|
|
|
|
|
} |
|
|
|
|
#define GG(a, b, c, d, x, s, ac) { \ |
|
|
|
|
(a) += G ((b), (c), (d)) + (x) + ac; \
|
|
|
|
|
(a) = ROTATELEFT ((a), (s)); \
|
|
|
|
|
(a) += (b); \
|
|
|
|
|
} |
|
|
|
|
#define HH(a, b, c, d, x, s, ac) { \ |
|
|
|
|
(a) += H ((b), (c), (d)) + (x) + ac; \
|
|
|
|
|
(a) = ROTATELEFT ((a), (s)); \
|
|
|
|
|
(a) += (b); \
|
|
|
|
|
} |
|
|
|
|
#define II(a, b, c, d, x, s, ac) { \ |
|
|
|
|
(a) += I ((b), (c), (d)) + (x) + ac; \
|
|
|
|
|
(a) = ROTATELEFT ((a), (s)); \
|
|
|
|
|
(a) += (b); \
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* Define of btye.*/ |
|
|
|
|
typedef unsigned char byte; |
|
|
|
|
/* Define of byte. */ |
|
|
|
@ -144,7 +80,7 @@ namespace ovo{ |
|
|
|
|
/**
|
|
|
|
|
* Math operation |
|
|
|
|
* |
|
|
|
|
* @author yimian, Jiewei Wei(md5) |
|
|
|
|
* @author yimian, Jiewei Wei(md5), Rene Nyffenegger(base64) |
|
|
|
|
* @category ovo |
|
|
|
|
* @package ovo |
|
|
|
|
*/ |
|
|
|
@ -161,6 +97,9 @@ namespace ovo{ |
|
|
|
|
string base64_encode(const string fromStr); |
|
|
|
|
string base64_decode(string const& s); |
|
|
|
|
|
|
|
|
|
/*** sha256 ***/ |
|
|
|
|
string sha256(string input); |
|
|
|
|
|
|
|
|
|
class MD5 { |
|
|
|
|
|
|
|
|
|
public: |
|
|
|
@ -206,8 +145,128 @@ namespace ovo{ |
|
|
|
|
return (isalnum(c) || (c == '+') || (c == '/')); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
class SHA256 { |
|
|
|
|
protected: |
|
|
|
|
typedef unsigned char uint8; |
|
|
|
|
typedef unsigned int uint32; |
|
|
|
|
typedef unsigned long long uint64; |
|
|
|
|
|
|
|
|
|
const static uint32 sha256_k[]; |
|
|
|
|
static const unsigned int SHA224_256_BLOCK_SIZE = (512/8); |
|
|
|
|
public: |
|
|
|
|
void init(); |
|
|
|
|
void update(const unsigned char *message, unsigned int len); |
|
|
|
|
void final(unsigned char *digest); |
|
|
|
|
static const unsigned int DIGEST_SIZE = ( 256 / 8); |
|
|
|
|
|
|
|
|
|
protected: |
|
|
|
|
void transform(const unsigned char *message, unsigned int block_nb); |
|
|
|
|
unsigned int m_tot_len; |
|
|
|
|
unsigned int m_len; |
|
|
|
|
unsigned char m_block[2*SHA224_256_BLOCK_SIZE]; |
|
|
|
|
uint32 m_h[8]; |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
}; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* Parameters of md5. */ |
|
|
|
|
#define s11 7 |
|
|
|
|
#define s12 12 |
|
|
|
|
#define s13 17 |
|
|
|
|
#define s14 22 |
|
|
|
|
#define s21 5 |
|
|
|
|
#define s22 9 |
|
|
|
|
#define s23 14 |
|
|
|
|
#define s24 20 |
|
|
|
|
#define s31 4 |
|
|
|
|
#define s32 11 |
|
|
|
|
#define s33 16 |
|
|
|
|
#define s34 23 |
|
|
|
|
#define s41 6 |
|
|
|
|
#define s42 10 |
|
|
|
|
#define s43 15 |
|
|
|
|
#define s44 21 |
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @Basic ovo::math::md5 functions. |
|
|
|
|
* |
|
|
|
|
* @param there bit32. |
|
|
|
|
* |
|
|
|
|
* @return one bit32. |
|
|
|
|
*/ |
|
|
|
|
#define F(x, y, z) (((x) & (y)) | ((~x) & (z))) |
|
|
|
|
#define G(x, y, z) (((x) & (z)) | ((y) & (~z))) |
|
|
|
|
#define H(x, y, z) ((x) ^ (y) ^ (z)) |
|
|
|
|
#define I(x, y, z) ((y) ^ ((x) | (~z))) |
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @Rotate Left. |
|
|
|
|
* |
|
|
|
|
* @param {num} the raw number. |
|
|
|
|
* |
|
|
|
|
* @param {n} rotate left n. |
|
|
|
|
* |
|
|
|
|
* @return the number after rotated left. |
|
|
|
|
*/ |
|
|
|
|
#define ROTATELEFT(num, n) (((num) << (n)) | ((num) >> (32-(n)))) |
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @Transformations for rounds 1, 2, 3, and 4. |
|
|
|
|
*/ |
|
|
|
|
#define FF(a, b, c, d, x, s, ac) { \ |
|
|
|
|
(a) += F ((b), (c), (d)) + (x) + ac; \
|
|
|
|
|
(a) = ROTATELEFT ((a), (s)); \
|
|
|
|
|
(a) += (b); \
|
|
|
|
|
} |
|
|
|
|
#define GG(a, b, c, d, x, s, ac) { \ |
|
|
|
|
(a) += G ((b), (c), (d)) + (x) + ac; \
|
|
|
|
|
(a) = ROTATELEFT ((a), (s)); \
|
|
|
|
|
(a) += (b); \
|
|
|
|
|
} |
|
|
|
|
#define HH(a, b, c, d, x, s, ac) { \ |
|
|
|
|
(a) += H ((b), (c), (d)) + (x) + ac; \
|
|
|
|
|
(a) = ROTATELEFT ((a), (s)); \
|
|
|
|
|
(a) += (b); \
|
|
|
|
|
} |
|
|
|
|
#define II(a, b, c, d, x, s, ac) { \ |
|
|
|
|
(a) += I ((b), (c), (d)) + (x) + ac; \
|
|
|
|
|
(a) = ROTATELEFT ((a), (s)); \
|
|
|
|
|
(a) += (b); \
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*** sha256 define ***/ |
|
|
|
|
#define SHA2_SHFR(x, n) (x >> n) |
|
|
|
|
#define SHA2_ROTR(x, n) ((x >> n) | (x << ((sizeof(x) << 3) - n))) |
|
|
|
|
#define SHA2_ROTL(x, n) ((x << n) | (x >> ((sizeof(x) << 3) - n))) |
|
|
|
|
#define SHA2_CH(x, y, z) ((x & y) ^ (~x & z)) |
|
|
|
|
#define SHA2_MAJ(x, y, z) ((x & y) ^ (x & z) ^ (y & z)) |
|
|
|
|
#define SHA256_F1(x) (SHA2_ROTR(x, 2) ^ SHA2_ROTR(x, 13) ^ SHA2_ROTR(x, 22)) |
|
|
|
|
#define SHA256_F2(x) (SHA2_ROTR(x, 6) ^ SHA2_ROTR(x, 11) ^ SHA2_ROTR(x, 25)) |
|
|
|
|
#define SHA256_F3(x) (SHA2_ROTR(x, 7) ^ SHA2_ROTR(x, 18) ^ SHA2_SHFR(x, 3)) |
|
|
|
|
#define SHA256_F4(x) (SHA2_ROTR(x, 17) ^ SHA2_ROTR(x, 19) ^ SHA2_SHFR(x, 10)) |
|
|
|
|
#define SHA2_UNPACK32(x, str) \ |
|
|
|
|
{ \
|
|
|
|
|
*((str) + 3) = (uint8) ((x) ); \
|
|
|
|
|
*((str) + 2) = (uint8) ((x) >> 8); \
|
|
|
|
|
*((str) + 1) = (uint8) ((x) >> 16); \
|
|
|
|
|
*((str) + 0) = (uint8) ((x) >> 24); \
|
|
|
|
|
} |
|
|
|
|
#define SHA2_PACK32(str, x) \ |
|
|
|
|
{ \
|
|
|
|
|
*(x) = ((uint32) *((str) + 3) ) \
|
|
|
|
|
| ((uint32) *((str) + 2) << 8) \
|
|
|
|
|
| ((uint32) *((str) + 1) << 16) \
|
|
|
|
|
| ((uint32) *((str) + 0) << 24); \
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#endif |