搜索引擎的那些事(32位MD5算法)对于学过密码学的同学来说,md5算法肯定不会很陌生。但是,对于我来说,md5是一个新的命题。那什么是md5呢?md5就是对已有的数据进行加密处理。当然,它还有别的用处,什么呢?比如说,可以验证下载的软件是否完整,可以验证保存的字符串是否发生重名等等。我在这里提到这个算法,主要是为了后面一个目的,防止url重名使用的。整个算法的内容其实比较复杂的,我们自己只要学会使用就可以了。这里使用的就两个文件,一个是md5.h,另外一个是md5.c,头文件内容如下所示,1/*Seemd5.cforexplanationandcopyrightinformation.*/23#ifndefMD5_H4#defineMD5_H56/*Unlikepreviousversionsofthiscode,uint32neednotbeexactly732bits,merely32bitsormore.Choosingadatatypewhichis328bitsinsteadof64isnotimportant;speedisconsiderablymore9important.ANSIguaranteesthat"unsignedlong"willbebigenough,10andalwaysusingitseemstohavefewdisadvantages.*/11typedefunsignedlonguint32;1213structMD5Context{14uint32buf[4];15uint32bits[2];16unsignedcharin[64];17};1819voidMD5Init(structMD5Context*context);20voidMD5Update(structMD5Context*context,unsignedcharconst*buf,unsignedlen);21voidMD5Final(unsignedchardigest[16],structMD5Context*context);22voidMD5Transform(uint32buf[4],constunsignedcharin[64]);2324/*25*ThisisneededtomakeRSAREFhappyonsomeMS-DOScompilers.26*/27typedefstructMD5ContextMD5_CTX;2829#endif/*!MD5_H*/源文件内容如下所示,30/*31*ThiscodeimplementstheMD5message-digestalgorithm.32*ThealgorithmisduetoRonRivest.Thiscodewas33*writtenbyColinPlumbin1993,nocopyrightisclaimed.34*Thiscodeisinthepublicdomain;dowithitwhatyouwish.35*36*EquivalentcodeisavailablefromRSADataSecurity,Inc.37*Thiscodehasbeentestedagainstthat,andisequivalent,38*exceptthatyoudon'tneedtoincludetwopagesoflegalese39*witheverycopy.40*41*Tocomputethemessagedigestofachunkofbytes,declarean42*MD5Contextstructure,passittoMD5Init,callMD5Updateas43*neededonbuffersfullofbytes,andthencallMD5Final,which44*willfillasupplied16-bytearraywiththedigest.45*/4647/*Thiscodewasmodifiedin1997byJimKingdonofCyclicSoftwareto48notrequireanintegertypewhichisexactly32bits.Thiswork49drawsonthechangesforthesamepurposebyTatuYlonen50<ylo@cs.hut.fi>aspartofSSH,butsinceIdidn'tactuallyuse51thatcode,thereisnocopyrightissue.Iherebydisclaim52copyrightinanychangesIhavemade;thiscoderemainsinthe53publicdomain.*/5455#include<stdio.h>56#include<memory.h>57#include<string.h>58#include<sys/types.h>5960#include"md5.h"6162/*Little-endianbyte-swappingroutines.Notethatthesedonot63dependonthesizeofdatatypessuchasuint32,nordotheyrequire64ustodetecttheendiannessofthemachinewearerunningon.It65ispossibletheyshouldbemacrosforspeed,butIwouldbe66surprisediftheywereaperformancebottleneckforMD5.*/6768staticuint3269getu32(addr)70constunsignedchar*addr;71{72return(((((unsignedlong)addr[3]<<8)|addr[2])<<8)73|addr[1])<<8|addr[0];74}7576staticvoid77putu32(data,addr)78uint32data;79unsignedchar*addr;80{81addr[0]=(unsignedchar)data;82addr[1]=(unsignedchar)(data>>8);83addr[2]=(unsignedchar)(data>>16);84addr[3]=(unsignedchar)(data>>24);85}8687/*88*StartMD5accumulation.Setbitcountto0andbuffertomysterious89*initializationconstants.90*/91void92MD5Init(ctx)93structMD5Context*ctx;94{95ctx->buf[0]=0x67452301;96ctx->buf[1]=0xefcdab89;97ctx->buf[2]=0x98badcfe;98ctx->buf[3]=0x10325476;99100ctx->bits[0]=0;101ctx->bits[1]=0;102}103104/*105*Updatecontexttoreflecttheconcatenationofanotherbufferfull106*ofbytes.107*/108void109MD5Update(...