md5摘要演算法(md5 message-digest algorithm)是一種常用的單向雜湊演算法(單向加密,不可逆,也成為hash演算法)。md5摘要演算法可將任意長度的明文資料壓縮為某一固定長度(通常為128bit,16位元組)。對於任意明文資料,產生的摘要資訊也是確定唯一的,因此md5摘要演算法常用於檔案資訊完整性的校驗。
軟體實現md5摘要演算法如下(詳細流程說明再做補充):
#include
#include
#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
#define proto_list(list) list
/* f, g, h and i are basic md5 functions.*/
#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 rotates x left n bits.*/
#define rotate_left(x, n) (((x) << (n)) | ((x) >> (32-(n))))
/* ff, gg, hh, and ii transformations for rounds 1, 2, 3, and 4.
rotation is separate from addition to prevent recomputation.*/
#define ff(a, b, c, d, x, s, ac)
#define gg(a, b, c, d, x, s, ac)
#define hh(a, b, c, d, x, s, ac)
#define ii(a, b, c, d, x, s, ac)
/* md5 context. */
typedef
struct
md5_ctx;
uint8_t padding[64]
=;// 四個32bits數,用於存放最終計算得到的訊息摘要。當訊息長度〉512bits時,
// 也用於存放每個512bits的中間結果
static
void
decode
(uint32_t *output,
const uint8_t *input, uint32_t len)
}static
void
encode
(uint8_t *output,
const uint32_t *input, uint32_t len)
}/* md5 basic transformation. transforms state based on block.*/
static
void
transform
(uint32_t state[4]
,const uint8_t block[64]
)void
md5init
(md5_ctx *context)
void
md5update
(md5_ctx *context,
const uint8_t *input, uint32_t inputlen)
context->count[1]
+=((uint32_t)inputlen >>29)
; partlen =
64- index;
/* transform as many times as possible.*/
if(inputlen >= partlen)
index =0;
}else
/* buffer remaining input */
memcpy
((uint8_t *
)&context->buffer[index]
,(uint8_t *
)&input[i]
, inputlen - i);}
void
md5final
(uint8_t digest[16]
, md5_ctx *context)
void
md5computing
(const uint8_t *text, uint16_t length, uint8_t *result)
MD5演算法實現
md5.h ifndef md5 h define md5 h typedef struct md5 ctx 非線性輔助函式 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...
MD5演算法實現
md5.h ifndef md5 h define md5 h typedef struct md5 ctx 非線性輔助函式 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...
java實現MD5演算法
public class md5 src class md5 static final byte padding private long state private long count private byte buffer public string digesthexstr private ...