mirror of
https://github.com/fumiama/simple-crypto.git
synced 2026-06-12 06:50:24 +08:00
md5 perf
This commit is contained in:
50
md5.c
50
md5.c
@@ -2,12 +2,7 @@
|
|||||||
|
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#if !__APPLE__
|
#include <stdint.h>
|
||||||
#include <endian.h>
|
|
||||||
#else
|
|
||||||
#include <machine/endian.h>
|
|
||||||
#endif
|
|
||||||
#include "simplecrypto.h"
|
|
||||||
|
|
||||||
// Constants are the integer part of the sines of integers (in radians) * 2^32.
|
// Constants are the integer part of the sines of integers (in radians) * 2^32.
|
||||||
const static uint32_t k[64] = {
|
const static uint32_t k[64] = {
|
||||||
@@ -39,59 +34,47 @@ const static uint32_t r[] = {7, 12, 17, 22, 7, 12, 17, 22, 7, 12, 17, 22, 7, 12,
|
|||||||
|
|
||||||
static void to_bytes(uint32_t val, uint8_t *bytes) {
|
static void to_bytes(uint32_t val, uint8_t *bytes) {
|
||||||
#ifdef WORDS_BIGENDIAN
|
#ifdef WORDS_BIGENDIAN
|
||||||
#if __APPLE__
|
*(uint32_t*)bytes = __builtin_bswap32(val);
|
||||||
val = __DARWIN_OSSwapInt32(val);
|
#else
|
||||||
#else
|
*(uint32_t*)bytes = val;
|
||||||
val = __builtin_bswap32(val);
|
|
||||||
#endif
|
|
||||||
#endif
|
#endif
|
||||||
*(uint32_t*)bytes = val;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static uint32_t to_int32(const uint8_t *bytes) {
|
static uint32_t to_uint32(const uint8_t *bytes) {
|
||||||
#ifdef WORDS_BIGENDIAN
|
#ifdef WORDS_BIGENDIAN
|
||||||
#if __APPLE__
|
return __builtin_bswap32(*(uint32_t*)bytes);
|
||||||
uint32_t val = __DARWIN_OSSwapInt32(*(uint32_t*)bytes);
|
|
||||||
#else
|
|
||||||
uint32_t val = __builtin_bswap32(*(uint32_t*)bytes);
|
|
||||||
#endif
|
|
||||||
#else
|
#else
|
||||||
uint32_t val = *(uint32_t*)bytes;
|
return *(uint32_t*)bytes;
|
||||||
#endif
|
#endif
|
||||||
return val;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
uint8_t* md5(const uint8_t *data, size_t data_len, uint8_t digest[16]) {
|
uint8_t* md5(const uint8_t *data, size_t data_len, uint8_t digest[16]) {
|
||||||
|
uint32_t w[16];
|
||||||
|
|
||||||
// These vars will contain the hash
|
// These vars will contain the hash
|
||||||
uint32_t h0, h1, h2, h3;
|
// Initialize variables - simple count in nibbles:
|
||||||
|
uint32_t h0 = 0x67452301;
|
||||||
|
uint32_t h1 = 0xefcdab89;
|
||||||
|
uint32_t h2 = 0x98badcfe;
|
||||||
|
uint32_t h3 = 0x10325476;
|
||||||
|
|
||||||
// Message (to prepare)
|
// Message (to prepare)
|
||||||
uint8_t *msg = NULL;
|
uint8_t *msg = NULL;
|
||||||
|
|
||||||
size_t new_len, offset;
|
size_t new_len, offset;
|
||||||
uint32_t w[16];
|
|
||||||
uint32_t a, b, c, d, i, f, g, temp;
|
uint32_t a, b, c, d, i, f, g, temp;
|
||||||
|
|
||||||
// Initialize variables - simple count in nibbles:
|
|
||||||
h0 = 0x67452301;
|
|
||||||
h1 = 0xefcdab89;
|
|
||||||
h2 = 0x98badcfe;
|
|
||||||
h3 = 0x10325476;
|
|
||||||
|
|
||||||
//Pre-processing:
|
//Pre-processing:
|
||||||
//append "1" bit to message
|
//append "1" bit to message
|
||||||
//append "0" bits until message length in bits ≡ 448 (mod 512)
|
//append "0" bits until message length in bits ≡ 448 (mod 512)
|
||||||
//append length mod (2^64) to message
|
//append length mod (2^64) to message
|
||||||
|
|
||||||
for (new_len = data_len + 1; new_len % (512/8) != 448/8; new_len++)
|
for (new_len = data_len + 1; new_len % (512/8) != 448/8; new_len++);
|
||||||
;
|
|
||||||
|
|
||||||
msg = (uint8_t*)malloc(new_len + 8);
|
msg = (uint8_t*)malloc(new_len + 8);
|
||||||
memcpy(msg, data, data_len);
|
memcpy(msg, data, data_len);
|
||||||
msg[data_len] = 0x80; // append the "1" bit; most significant bit is "first"
|
msg[data_len] = 0x80; // append the "1" bit; most significant bit is "first"
|
||||||
for (offset = data_len + 1; offset < new_len; offset++)
|
memset(&msg[data_len+1], 0, new_len-data_len-1); // append "0" bits
|
||||||
msg[offset] = 0; // append "0" bits
|
|
||||||
|
|
||||||
// append the len in bits at the end of the buffer.
|
// append the len in bits at the end of the buffer.
|
||||||
to_bytes(data_len*8, msg + new_len);
|
to_bytes(data_len*8, msg + new_len);
|
||||||
@@ -101,10 +84,9 @@ uint8_t* md5(const uint8_t *data, size_t data_len, uint8_t digest[16]) {
|
|||||||
// Process the message in successive 512-bit chunks:
|
// Process the message in successive 512-bit chunks:
|
||||||
//for each 512-bit chunk of message:
|
//for each 512-bit chunk of message:
|
||||||
for(offset=0; offset<new_len; offset += (512/8)) {
|
for(offset=0; offset<new_len; offset += (512/8)) {
|
||||||
|
|
||||||
// break chunk into sixteen 32-bit words w[j], 0 ≤ j ≤ 15
|
// break chunk into sixteen 32-bit words w[j], 0 ≤ j ≤ 15
|
||||||
for (i = 0; i < 16; i++)
|
for (i = 0; i < 16; i++)
|
||||||
w[i] = to_int32(msg + offset + i*4);
|
w[i] = to_uint32(msg + offset + i*4);
|
||||||
|
|
||||||
// Initialize hash value for this chunk:
|
// Initialize hash value for this chunk:
|
||||||
a = h0;
|
a = h0;
|
||||||
|
|||||||
Reference in New Issue
Block a user