/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- * * The contents of this file are subject to the Netscape Public * License Version 1.1 (the "License"); you may not use this file * except in compliance with the License. You may obtain a copy of * the License at http://www.mozilla.org/NPL/ * * Software distributed under the License is distributed on an "AS * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or * implied. See the License for the specific language governing * rights and limitations under the License. * * The Original Code is mozilla.org code. * * The Initial Developer of the Original Code is Netscape * Communications Corporation. Portions created by Netscape are * Copyright (C) 1998 Netscape Communications Corporation. All * Rights Reserved. * * Contributor(s): */ /* * MD5 digest implementation * * contributed by mrsam@geocities.com * */ /* for endian-ness */ //#include "prtypes.h" #include "string.h" //#include "nsMsgMD5.h" #define MD5_BYTE unsigned char #define MD5_WORD unsigned int extern "C" const void *nsMsgMD5Digest(const void *msg, unsigned int len); typedef union md5_endian { //#ifdef IS_LITTLE_ENDIAN MD5_WORD m_word; struct { MD5_BYTE m_0, m_1, m_2, m_3; } m_bytes; //#endif /*#ifdef IS_BIG_ENDIAN MD5_WORD m_word; struct { MD5_BYTE m_3, m_2, m_1, m_0; } m_bytes; #endif */ } ; static const MD5_BYTE *m_msg; static MD5_WORD m_msglen; static MD5_WORD m_msgpaddedlen; static MD5_BYTE m_pad[72]; static MD5_BYTE m_digest[16]; #define MD5_MSGBYTE(n) ((MD5_BYTE)((n) < m_msglen?m_msg[n]:m_pad[n-m_msglen])) inline void MD5_MSGWORD(MD5_WORD &n, MD5_WORD i) { union md5_endian e; i *= 4; e.m_bytes.m_0=MD5_MSGBYTE(i); ++i; e.m_bytes.m_1=MD5_MSGBYTE(i); ++i; e.m_bytes.m_2=MD5_MSGBYTE(i); ++i; e.m_bytes.m_3=MD5_MSGBYTE(i); n=e.m_word; } inline MD5_WORD MD5_ROL(MD5_WORD w, int n) { return ( w << n | ( (w) >> (32-n) ) ); } static MD5_WORD T[64]={ 0xd76aa478, 0xe8c7b756, 0x242070db, 0xc1bdceee, 0xf57c0faf, 0x4787c62a, 0xa8304613, 0xfd469501, 0x698098d8, 0x8b44f7af, 0xffff5bb1, 0x895cd7be, 0x6b901122, 0xfd987193, 0xa679438e, 0x49b40821, 0xf61e2562, 0xc040b340, 0x265e5a51, 0xe9b6c7aa, 0xd62f105d, 0x2441453, 0xd8a1e681, 0xe7d3fbc8, 0x21e1cde6, 0xc33707d6, 0xf4d50d87, 0x455a14ed, 0xa9e3e905, 0xfcefa3f8, 0x676f02d9, 0x8d2a4c8a, 0xfffa3942, 0x8771f681, 0x6d9d6122, 0xfde5380c, 0xa4beea44, 0x4bdecfa9, 0xf6bb4b60, 0xbebfbc70, 0x289b7ec6, 0xeaa127fa, 0xd4ef3085, 0x4881d05, 0xd9d4d039, 0xe6db99e5, 0x1fa27cf8, 0xc4ac5665, 0xf4292244, 0x432aff97, 0xab9423a7, 0xfc93a039, 0x655b59c3, 0x8f0ccc92, 0xffeff47d, 0x85845dd1, 0x6fa87e4f, 0xfe2ce6e0, 0xa3014314, 0x4e0811a1, 0xf7537e82, 0xbd3af235, 0x2ad7d2bb, 0xeb86d391}; const void *nsMsgMD5Digest(const void *msg, unsigned int len) { MD5_WORD i,j; union md5_endian e; MD5_WORD hilen, lolen; MD5_BYTE padlen[8]; m_msg=(const MD5_BYTE *)msg; m_msglen=len; m_msgpaddedlen = len+72; m_msgpaddedlen &= ~63; for (i=0; i<72; i++) m_pad[i]=0; m_pad[0]=0x80; lolen=len << 3; hilen=len >> 29; e.m_word=lolen; padlen[0]=e.m_bytes.m_0; padlen[1]=e.m_bytes.m_1; padlen[2]=e.m_bytes.m_2; padlen[3]=e.m_bytes.m_3; e.m_word=hilen; padlen[4]=e.m_bytes.m_0; padlen[5]=e.m_bytes.m_1; padlen[6]=e.m_bytes.m_2; padlen[7]=e.m_bytes.m_3; memcpy( &m_pad[m_msgpaddedlen - m_msglen - 8], padlen, 8); MD5_WORD A=0x67452301; MD5_WORD B=0xefcdab89; MD5_WORD C=0x98badcfe; MD5_WORD D=0x10325476; #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_WORD nwords= m_msgpaddedlen / 4, k=0; MD5_WORD x[16]; for (i=0; i