[Project64] MD5.cpp auto code clean up
This commit is contained in:
parent
17b24f299f
commit
22fc35d49c
|
@ -16,7 +16,6 @@
|
||||||
MD5C.C - RSA Data Security, Inc., MD5 message-digest algorithm
|
MD5C.C - RSA Data Security, Inc., MD5 message-digest algorithm
|
||||||
MDDRIVER.C - test driver for MD2, MD4 and MD5
|
MDDRIVER.C - test driver for MD2, MD4 and MD5
|
||||||
|
|
||||||
|
|
||||||
Copyright (C) 1991-2, RSA Data Security, Inc. Created 1991. All
|
Copyright (C) 1991-2, RSA Data Security, Inc. Created 1991. All
|
||||||
rights reserved.
|
rights reserved.
|
||||||
|
|
||||||
|
@ -46,23 +45,18 @@ documentation and/or software.
|
||||||
// MD5 simple initialization method
|
// MD5 simple initialization method
|
||||||
|
|
||||||
MD5::MD5(){
|
MD5::MD5(){
|
||||||
|
|
||||||
init();
|
init();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
MD5::~MD5()
|
MD5::~MD5()
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// MD5 block update operation. Continues an MD5 message-digest
|
// MD5 block update operation. Continues an MD5 message-digest
|
||||||
// operation, processing another message block, and updating the
|
// operation, processing another message block, and updating the
|
||||||
// context.
|
// context.
|
||||||
|
|
||||||
void MD5::update(const uint1 *input, uint4 input_length) {
|
void MD5::update(const uint1 *input, uint4 input_length) {
|
||||||
|
|
||||||
uint4 input_index, buffer_index;
|
uint4 input_index, buffer_index;
|
||||||
uint4 buffer_space; // how much space is left in buffer
|
uint4 buffer_space; // how much space is left in buffer
|
||||||
|
|
||||||
|
@ -80,7 +74,6 @@ void MD5::update (const uint1 *input, uint4 input_length) {
|
||||||
|
|
||||||
count[1] += ((uint4)input_length >> 29);
|
count[1] += ((uint4)input_length >> 29);
|
||||||
|
|
||||||
|
|
||||||
buffer_space = 64 - buffer_index; // how much space is left in buffer
|
buffer_space = 64 - buffer_index; // how much space is left in buffer
|
||||||
|
|
||||||
// Transform as many times as possible.
|
// Transform as many times as possible.
|
||||||
|
@ -99,18 +92,14 @@ void MD5::update (const uint1 *input, uint4 input_length) {
|
||||||
else
|
else
|
||||||
input_index = 0; // so we can buffer the whole input
|
input_index = 0; // so we can buffer the whole input
|
||||||
|
|
||||||
|
|
||||||
// and here we do the buffering:
|
// and here we do the buffering:
|
||||||
memcpy(buffer + buffer_index, (unsigned char *)(input + input_index), input_length - input_index);
|
memcpy(buffer + buffer_index, (unsigned char *)(input + input_index), input_length - input_index);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// MD5 update for files.
|
// MD5 update for files.
|
||||||
// Like above, except that it works on files (and uses above as a primitive.)
|
// Like above, except that it works on files (and uses above as a primitive.)
|
||||||
|
|
||||||
void MD5::update(FILE *file){
|
void MD5::update(FILE *file){
|
||||||
|
|
||||||
unsigned char buffer[1024];
|
unsigned char buffer[1024];
|
||||||
int len;
|
int len;
|
||||||
|
|
||||||
|
@ -124,16 +113,12 @@ void MD5::update(FILE *file){
|
||||||
} while (len);
|
} while (len);
|
||||||
|
|
||||||
fclose(file);
|
fclose(file);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// MD5 finalization. Ends an MD5 message-digest operation, writing the
|
// MD5 finalization. Ends an MD5 message-digest operation, writing the
|
||||||
// the message digest and zeroizing the context.
|
// the message digest and zeroizing the context.
|
||||||
|
|
||||||
|
|
||||||
void MD5::finalize(){
|
void MD5::finalize(){
|
||||||
|
|
||||||
unsigned char bits[8];
|
unsigned char bits[8];
|
||||||
unsigned int index, padLen;
|
unsigned int index, padLen;
|
||||||
static uint1 PADDING[64] = {
|
static uint1 PADDING[64] = {
|
||||||
|
@ -165,13 +150,9 @@ void MD5::finalize (){
|
||||||
memset(buffer, 0, sizeof(*buffer));
|
memset(buffer, 0, sizeof(*buffer));
|
||||||
|
|
||||||
finalized = 1;
|
finalized = 1;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
MD5::MD5(CPath File){
|
MD5::MD5(CPath File){
|
||||||
|
|
||||||
init(); // must be called be all constructors
|
init(); // must be called be all constructors
|
||||||
if (File.Exists())
|
if (File.Exists())
|
||||||
{
|
{
|
||||||
|
@ -184,9 +165,7 @@ MD5::MD5(CPath File){
|
||||||
finalize();
|
finalize();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
MD5::MD5(FILE *file){
|
MD5::MD5(FILE *file){
|
||||||
|
|
||||||
init(); // must be called be all constructors
|
init(); // must be called be all constructors
|
||||||
update(file);
|
update(file);
|
||||||
finalize();
|
finalize();
|
||||||
|
@ -254,8 +233,6 @@ const char *MD5::hex_digest()
|
||||||
|
|
||||||
// PRIVATE METHODS:
|
// PRIVATE METHODS:
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
void MD5::init(){
|
void MD5::init(){
|
||||||
finalized = 0; // we just started!
|
finalized = 0; // we just started!
|
||||||
|
|
||||||
|
@ -275,8 +252,6 @@ void MD5::init(){
|
||||||
m_hex_digest = NULL;
|
m_hex_digest = NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// Constants for MD5Transform routine.
|
// Constants for MD5Transform routine.
|
||||||
// Although we could use C++ style constants, defines are actually better,
|
// Although we could use C++ style constants, defines are actually better,
|
||||||
// since they let us easily evade scope clashes.
|
// since they let us easily evade scope clashes.
|
||||||
|
@ -298,12 +273,8 @@ void MD5::init(){
|
||||||
#define S43 15
|
#define S43 15
|
||||||
#define S44 21
|
#define S44 21
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// MD5 basic transformation. Transforms state based on block.
|
// MD5 basic transformation. Transforms state based on block.
|
||||||
void MD5::transform(uint1 block[64]){
|
void MD5::transform(uint1 block[64]){
|
||||||
|
|
||||||
uint4 a = state[0], b = state[1], c = state[2], d = state[3], x[16];
|
uint4 a = state[0], b = state[1], c = state[2], d = state[3], x[16];
|
||||||
|
|
||||||
decode(x, block, 64);
|
decode(x, block, 64);
|
||||||
|
@ -389,15 +360,11 @@ void MD5::transform (uint1 block[64]){
|
||||||
|
|
||||||
// Zeroize sensitive information.
|
// Zeroize sensitive information.
|
||||||
memset((uint1 *)x, 0, sizeof(x));
|
memset((uint1 *)x, 0, sizeof(x));
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// Encodes input (UINT4) into output (unsigned char). Assumes len is
|
// Encodes input (UINT4) into output (unsigned char). Assumes len is
|
||||||
// a multiple of 4.
|
// a multiple of 4.
|
||||||
void MD5::encode(uint1 *output, uint4 *input, uint4 len) {
|
void MD5::encode(uint1 *output, uint4 *input, uint4 len) {
|
||||||
|
|
||||||
unsigned int i, j;
|
unsigned int i, j;
|
||||||
|
|
||||||
for (i = 0, j = 0; j < len; i++, j += 4) {
|
for (i = 0, j = 0; j < len; i++, j += 4) {
|
||||||
|
@ -408,13 +375,9 @@ void MD5::encode (uint1 *output, uint4 *input, uint4 len) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// Decodes input (unsigned char) into output (UINT4). Assumes len is
|
// Decodes input (unsigned char) into output (UINT4). Assumes len is
|
||||||
// a multiple of 4.
|
// a multiple of 4.
|
||||||
void MD5::decode(uint4 *output, uint1 *input, uint4 len){
|
void MD5::decode(uint4 *output, uint1 *input, uint4 len){
|
||||||
|
|
||||||
unsigned int i, j;
|
unsigned int i, j;
|
||||||
|
|
||||||
for (i = 0, j = 0; j < len; i++, j += 4)
|
for (i = 0, j = 0; j < len; i++, j += 4)
|
||||||
|
@ -422,41 +385,28 @@ void MD5::decode (uint4 *output, uint1 *input, uint4 len){
|
||||||
(((uint4)input[j + 2]) << 16) | (((uint4)input[j + 3]) << 24);
|
(((uint4)input[j + 2]) << 16) | (((uint4)input[j + 3]) << 24);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// Note: Replace "for loop" with standard memcpy if possible.
|
// Note: Replace "for loop" with standard memcpy if possible.
|
||||||
void MD5::memcpy(uint1 *output, uint1 *input, uint4 len){
|
void MD5::memcpy(uint1 *output, uint1 *input, uint4 len){
|
||||||
|
|
||||||
unsigned int i;
|
unsigned int i;
|
||||||
|
|
||||||
for (i = 0; i < len; i++)
|
for (i = 0; i < len; i++)
|
||||||
output[i] = input[i];
|
output[i] = input[i];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// Note: Replace "for loop" with standard memset if possible.
|
// Note: Replace "for loop" with standard memset if possible.
|
||||||
void MD5::memset(uint1 *output, uint1 value, uint4 len){
|
void MD5::memset(uint1 *output, uint1 value, uint4 len){
|
||||||
|
|
||||||
unsigned int i;
|
unsigned int i;
|
||||||
|
|
||||||
for (i = 0; i < len; i++)
|
for (i = 0; i < len; i++)
|
||||||
output[i] = value;
|
output[i] = value;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// ROTATE_LEFT rotates x left n bits.
|
// ROTATE_LEFT rotates x left n bits.
|
||||||
|
|
||||||
inline unsigned int MD5::rotate_left(uint4 x, uint4 n){
|
inline unsigned int MD5::rotate_left(uint4 x, uint4 n){
|
||||||
return (x << n) | (x >> (32 - n));
|
return (x << n) | (x >> (32 - n));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// F, G, H and I are basic MD5 functions.
|
// F, G, H and I are basic MD5 functions.
|
||||||
|
|
||||||
inline unsigned int MD5::F(uint4 x, uint4 y, uint4 z){
|
inline unsigned int MD5::F(uint4 x, uint4 y, uint4 z){
|
||||||
|
@ -475,12 +425,9 @@ inline unsigned int MD5::I (uint4 x, uint4 y, uint4 z){
|
||||||
return y ^ (x | ~z);
|
return y ^ (x | ~z);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.
|
// FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.
|
||||||
// Rotation is separate from addition to prevent recomputation.
|
// Rotation is separate from addition to prevent recomputation.
|
||||||
|
|
||||||
|
|
||||||
inline void MD5::FF(uint4& a, uint4 b, uint4 c, uint4 d, uint4 x,
|
inline void MD5::FF(uint4& a, uint4 b, uint4 c, uint4 d, uint4 x,
|
||||||
uint4 s, uint4 ac){
|
uint4 s, uint4 ac){
|
||||||
a += F(b, c, d) + x + ac;
|
a += F(b, c, d) + x + ac;
|
||||||
|
|
Loading…
Reference in New Issue