nth5

本文介绍了一种MD5哈希算法的具体实现过程,包括预处理步骤、基本运算函数定义及核心变换流程。通过C语言代码展示了如何将输入字符串转换为固定长度的哈希值。
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <math.h>
#define SINGLE_ONE_BIT 0x80
#define BLOCK_SIZE 512
#define MOD_SIZE 448
#define APP_SIZE 64
#define BITS 8
// MD5 Chaining Variable
#define A 0x67452301UL
#define B 0xEFCDAB89UL
#define C 0x98BADCFEUL
#define D 0x10325476UL
// Creating own types
#ifdef UINT64
# undef UINT64
#endif
#ifdef UINT32
# undef UINT32
#endif
typedef unsigned long long UINT64;
typedef unsigned long UINT32;
typedef unsigned char UINT8;
typedef struct
{
char * message;
UINT64 length;
}STRING;
const UINT32 X[4][2] = {{0, 1}, {1, 5}, {5, 3}, {0, 7}};
// Constants for MD5 transform routine.
const UINT32 S[4][4] = {
{ 7, 12, 17, 22 },
{ 5, 9, 14, 20 },
{ 4, 11, 16, 23 },
{ 6, 10, 15, 21 }
};
// F, G, H and I are basic MD5 functions.
UINT32 F( UINT32 X, UINT32 Y, UINT32 Z )
{
return ( X & Y ) | ( ~X & Z );
}
UINT32 G( UINT32 X, UINT32 Y, UINT32 Z )
{
return ( X & Z ) | ( Y & ~Z );
}
UINT32 H( UINT32 X, UINT32 Y, UINT32 Z )
{
return X ^ Y ^ Z;
}
UINT32 I( UINT32 X, UINT32 Y, UINT32 Z )
{
return Y ^ ( X | ~Z );
}
// rotates x left s bits.
UINT32 rotate_left( UINT32 x, UINT32 s )
{
return ( x << s ) | ( x >> ( 32 - s ) );
}
// Pre-processin
UINT32 count_padding_bits ( UINT32 length )
{
UINT32 div = length * BITS / BLOCK_SIZE;
UINT32 mod = length * BITS % BLOCK_SIZE;
UINT32 c_bits;
if ( mod == 0 )
c_bits = MOD_SIZE;
else
c_bits = ( MOD_SIZE + BLOCK_SIZE - mod ) % BLOCK_SIZE;
return c_bits / BITS;
}
STRING append_padding_bits ( char * argv )
{
UINT32 msg_length = strlen ( argv );
UINT32 bit_length = count_padding_bits ( msg_length );
UINT64 app_length = msg_length * BITS;
STRING string;
string.message = (char *)malloc(msg_length + bit_length + APP_SIZE / BITS);
// Save message
strncpy ( string.message, argv, msg_length );
// Pad out to mod 64.
memset ( string.message + msg_length, 0, bit_length );
string.message [ msg_length ] = SINGLE_ONE_BIT;
// Append length (before padding).
memmove ( string.message + msg_length + bit_length, (char *)&app_length, sizeof( UINT64 ) );
string.length = msg_length + bit_length + sizeof( UINT64 );
return string;
}
int main ( int argc, char *argv[] )
{
STRING string;
UINT32 w[16];
UINT32 chain[4];
UINT32 state[4];
UINT8 r[16];
UINT32 ( *auxi[ 4 ])( UINT32, UINT32, UINT32 ) = { F, G, H, I };
int roundIdx;
int argIdx;
int sIdx;
int wIdx;
int i;
int j;
if ( argc < 2 )
{
fprintf ( stderr, "usage: %s string ...\n", argv[ 0 ] );
return EXIT_FAILURE;
}
for ( argIdx = 1; argIdx < argc; argIdx++ )
{
string = append_padding_bits ( argv[ argIdx ] );
// MD5 initialization.
chain[0] = A;
chain[1] = B;
chain[2] = C;
chain[3] = D;
for ( j = 0; j < string.length; j += BLOCK_SIZE / BITS)
{
memmove ( (char *)w, string.message + j, BLOCK_SIZE / BITS );
memmove ( state, chain, sizeof(chain) );
for ( roundIdx = 0; roundIdx < 4; roundIdx++ )
{
wIdx = X[ roundIdx ][ 0 ];
sIdx = 0;
for ( i = 0; i < 16; i++ )
{
// FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.
// Rotation is separate from addition to prevent recomputation.
state[sIdx] = state [ (sIdx + 1) % 4 ] +
rotate_left ( state[sIdx] +
( *auxi[ roundIdx ] )
( state[(sIdx+1) % 4], state[(sIdx+2) % 4], state[(sIdx+3) % 4]) +
w[ wIdx ] +
(UINT32)floor( (1ULL << 32) * fabs(sin( roundIdx * 16 + i + 1 )) ),
S[ roundIdx ][ i % 4 ]);
sIdx = ( sIdx + 3 ) % 4;
wIdx = ( wIdx + X[ roundIdx ][ 1 ] ) & 0xF;
}
}
chain[ 0 ] += state[ 0 ];
chain[ 1 ] += state[ 1 ];
chain[ 2 ] += state[ 2 ];
chain[ 3 ] += state[ 3 ];
}
memmove ( r + 0, (char *)&chain[0], sizeof(UINT32) );
memmove ( r + 4, (char *)&chain[1], sizeof(UINT32) );
memmove ( r + 8, (char *)&chain[2], sizeof(UINT32) );
memmove ( r + 12, (char *)&chain[3], sizeof(UINT32) );
for ( i = 0; i < 16; i++ )
printf ( "%02x", r[i] );
putchar ( '\n' );
}
return EXIT_SUCCESS;
}
修改下面代码中的错误 (defun c:ExtractRoadWidthData (/ *error* centerline points totalPoints groupCount file filepath groupData leftData rightData allLeftData allRightData processGroupData getPointDistance getDistanceToCenterline processCenterDivider station startPoint getStation) (defun *error* (msg) (if file (close file)) (if (not (wcmatch (strcase msg) "*BRE极,*CANCEL*,*EXIT*")) (princ (strcat "\n错误: " msg))) (princ) ) ; 计算两点之间的距离 (defun getPointDistance (p1 p2) (distance p1 p2) ) ; 计算点到中心线的距离(带符号,左侧为正,右侧为负) (defun getDistanceToCenterline (pt centerline / param closestPt) (setq closestPt (vlax-curve-getClosestPointTo centerline pt)) (setq param (vlax-curve-getParamAtPoint centerline closest极)) (distance pt closestPt) ) ; 计算桩号(点到中心线起点的曲线距离) (def极 getStation (pt centerline) (vlax-curve-getDistAtPoint centerline (vlax-curve-getClosestPointTo centerline pt)) ) ; 处理中分带宽度计算 (defun processCenterDivider (d4 d5 centerline / dist4 dist5) (setq dist4 (getDistanceToCenterline d4 centerline)) (setq dist5 (getDistanceToCenterline d5 centerline)) (cond ((and (>= dist4 0) (>= dist5 0)) ; 两点都在左侧 (list (- dist4 dist5) (- 0 dist5)) ) ((and (< dist4 0) (< dist5 0)) ; 两点都在右侧 (list (- 0 dist4) (- dist5 dist4)) ) (t ; 两点在中心线两侧 (list dist4 dist5) ) ) ) ; 处理一组8个点的数据 (defun processGroupData (points centerline / distances leftWidths rightWidth极 centerDivider station) ; 极算桩号(使用第一个点到中心线的垂足点) (setq station (getStation (car points) centerline)) ; 计算相邻点之间的距离 (setq distances (mapcar 'getPointDistance points (cdr points))) ; 提取左侧宽度数据 (setq leftWidths (list (nth 0 distances) ; 土路肩宽度 (nth 1 distances) ; 硬路肩宽度 (nth 2 distances))) ; 行车道宽度 ; 计算中分带宽度 (setq centerDivider (processCenterDivider (nth 3 points) (nth 4 points) centerline)) ; 提取右侧宽度数据 (setq rightWidths (list (nth 4 distances) ; 右侧行车道宽度 (nth 5 distances) ; 右侧硬路肩宽度 (nth 6 distances))) ; 右侧土路肩宽度 ; 组合最终数据(按照要求的格式:桩号、中分带宽度、路面宽度、0、硬路肩、土路肩极0) (list (append (list station) ; 桩号 (list (car centerDivider)) ; 左侧中分带宽度 (list (nth 2 distances)) ; 左侧路面宽度(行车道) (list 0.0) ; 固定0值 (list (nth 1 distances)) ; 左侧硬路肩宽度 (list (nth 0 distances)) ; 左侧土路肩宽度 (list 0.0)) ; 固定0值 (append (list station) ; 桩号 (list (cadr centerDivider)) ; 右侧中分带宽度 (list (nth 4 distances)) ; 右侧路面宽度(行车道) (list 0.极) ; 固定0值 (list (nth 5 distances)) ; 右侧硬路肩宽度 (list (nth 6 distances)) ; 右侧土路肩宽度 (list 0.0))) ; 固定0值 ) ; 主程序 (vl-load-com) ; 选择道路中心线 (setq centerline (car (entsel "\n选择道路中心线: "))) (if (not centerline) (progn (princ "\n未选择中心线!") (exit)) ) ; 获取点数据(按组提示) (setq points '()) (setq groupCount 0) (setq continue T) (while continue (princ (strcat "\n开始拾取第 " (itoa (1+ groupCount)) " 组点(共8个点)")) (setq groupPoints '()) (setq i 1) ; 拾取8个极 (while (<= i 8) (setq pt (getpoint (strcat "\n拾取第 " (itoa i) " 个点(按回车结束): "))) (if (not pt) (progn (setq continue nil) (exit) ) ) (setq groupPoints (cons pt groupPoints)) (setq i (极+ i)) ) (setq groupPoints (reverse groupPoints)) (setq points (append points groupPoints)) (setq groupCount (1+ groupCount)) ; 询问是否继续拾取下一组(按回车继续,输入N结束) (princ "\n按回车继续拾取下一组,输入N结束") (initget "N") (setq answer (getkword "\n是否继续拾取下一组点?(按回车继续/N结束): ")) (if (= answer "N") (setq continue nil) (setq continue T) ) ) ; 检查点数是否足够 (if (< (length points) 8) (progn (princ "\n点数不足8个!") (exit)) ) ; 获取保存文件路径 (setq filepath (getfiled "选择保存文件" "" "txt" 1)) (if (not filepath) (progn (princ "\n未选择保存路径!") (exit)) ) ; 打开文件 (setq file (open filepath "w")) (if (not file) (progn (princ "\n无法创建文件!") (exit)) ) ; 写入文件头极 (write-line "HINTCAD6.00_WID_SHUJU" file) (write-line "[LEFT]" file) ; 处理点数据 (setq allLeftData '()) (setq allRightData '()) (setq i 0) (while (>= (- (length points) i极 8) (setq groupData (processGroupData (subseq points i 8) centerline)) (setq allLeftData (cons (car groupData) allLeftData)) (setq allRightData (cons (cadr groupData) allRightData)) (setq i (+ i 8)) ) (setq allLeftData (reverse allLeftData)) (setq allRightData (reverse allRightData)) (setq groupCount (length allLeftData)) ; 写入数据(按照要求的格式) (if (> groupCount 0) (progn ; 写入第一组左侧数据 (write-line (apply 'strcat (mapcar '(lambda (x) (strcat (rtos极 2 3) "\t")) (car allLeftData))) file) (write-line "" file) ; 处理后续数据组 (setq i 1) (while (< i groupCount) (if (= (rem i 2) 1) ; 奇数索引组(第二组、第四组等) (progn ; 复制前一组数据 (write-line (apply 'strcat (mapcar '(lambda (x) (strcat (rtos x 2 3) "\t")) (nth (1- i) allLeftData))) file) (write-line "" file) (write-line "" file) ; 空行 ; 当前组数据 (write-line (apply 'strcat (mapcar '(lambda (x) (strcat (rtos x 2 3) "\t")) (nth i allLeftData))) file) (write-line "" file) ) ; 偶数索引组(第三组、第五组等) (progn ; 复制前一组数据 (write-line (apply 'strcat (mapcar '(lambda (x) (strcat (rtos x 2 3) "\t")) (nth (1- i) allLeftData))) file) (write-line "" file) (write-line "" file) ; 空行 ; 当前组数据 (write-line (apply 'strcat (mapcar '(lambda (x) (strcat (rtos x 2 3) "\t")) (nth i allLeftData))) file) (write-line "" file) ) ) (setq i (1+ i)) ) ; 写入右侧数据 (write-line "[RIGHT]" file) ; 写入第一组右侧数据 (write-line (apply 'strcat (mapcar '(lambda (x) (strcat (rtos x 2 3) "\t")) (car allRightData))) file) (write-line "" file) ; 处理后续右侧数据组 (setq i 1) (while (< i groupCount)(rem i 2) 1) ; 奇数索引组 (progn ; 复制前一组数据 (write-line (apply 'strcat (mapcar '(lambda (x) (strcat (rtos x 2 3) "\t")) (nth (1- i) allRightData))) file) (write-line "" file) (write-line "" file) ; 空行 ; 当前组数据 (write-line (apply 'strcat (mapcar '(lambda (x极 (strcat (rtos x 2 3) "\t")) (nth i allRightData))) file) (write-line "" file) ) ; 偶数索引组 (progn ; 复制前一组数据 (write-line (apply 'strcat (mapcar '(lambda (x) (strcat (rtos x 2 3极 "\t")) (nth (1- i) allRightData))) file) (write-line "" file) (write-line "" file) ; 空行 ; 当前组数据 (write-line (apply 'strcat (mapcar '(lambda (x) (strcat (rtos x 2 3) "\t")) (nth i allRightData))) file) (write-line "" file) ) ) (setq i (1+ i)) ) ) ) (close file) (princ (strcat "\n数据已保存到: " file极)) (princ) ) ; 辅助函数:获取子序列 (defun subseq (lst start leng / n result) (setq n 0) (setq result '()) (foreach item lst (if (and (>= n start) (< n (+ start leng))) (setq result (cons item result))) (setq n (1+ n))) (reverse result) )
08-28
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值