This commit is contained in:
varada%netscape.com 1999-04-20 20:29:43 +00:00
Родитель 960a74971b
Коммит 20e44d1110
5 изменённых файлов: 571 добавлений и 0 удалений

Просмотреть файл

@ -0,0 +1,24 @@
#include <stdio.h>
#include <stdlib.h>
#define MD5_WORD unsigned int
union {
char bytes[4];
MD5_WORD n;
} u;
void main()
{
u.n=0x03020100;
if (u.bytes[0] == 3)
printf("#define MD5_BIG_ENDIAN\n");
else if (u.bytes[0] == 0)
printf("#define MD5_LITTLE_ENDIAN\n");
else
{
printf("#error No endians!\n");
exit(1);
}
exit (0);
}

Просмотреть файл

@ -0,0 +1 @@
#define IS_LITTLE_ENDIAN

Просмотреть файл

@ -0,0 +1,249 @@
//#define MD 5
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
#include <sys/types.h>
//#include "global.h"
//#include "md5.h"
//#include "md5c.c"
//#include "nsMsgMD5.h"
//#define MD5_LENGTH 16
#define OBSCURE_CODE 7
const void *nsMsgMD5Digest(const void *msg, unsigned int len);
static void MDString (unsigned char *, char *);
//static void MDFile (unsigned char *, char *);
static void MDPrint (char *, char *, unsigned char *, long);
void obscure (const char *, char *, int);
//#define MD_CTX MD5_CTX
//#define MDInit MD5Init
//#define MDUpdate MD5Update
//#define MDFinal MD5Final
// Main driver.
short bflag = 1; /* 1 == print sums in binary */
int main (argc, argv)
int argc;
char *argv[];
{
char outputfile[] = "netscape.cfg";
unsigned char* digest;//[MD5_LENGTH];
long f_size=0;
int index=0;
int num=0;
char *file_buffer;
char *hash_input;
char final_buf[50];
char final_hash[49];
char *magic_key = "VonGloda5652TX75235ISBN";
unsigned int key_len =(strlen (magic_key)+1);
FILE *outp;
FILE *input_file;
unsigned int len_buffer;
printf ("before opening the file \n");
if ((input_file = fopen (argv[1], "rb")) == NULL){
printf ("%s can't be opened for reading\n", argv[1]);
} else { printf ("after opening the file \n");
fseek(input_file, 0,2);
f_size = ftell(input_file);
fseek (input_file,0,0);
file_buffer = (char *) malloc (f_size);
hash_input = (char *) malloc (f_size +key_len);
fread (file_buffer,1,f_size,input_file);
file_buffer[f_size]=NULL;
printf ("%s is the statement \n", magic_key);
strcpy (hash_input , file_buffer);
printf ("%s is 2 hash input statement \n",hash_input);
// printf ("%s\n",file_buffer);
// strncat (hash_input,magic_key,key_len);
// printf ("%s is 1 hash input statement \n",hash_input);
// printf ("%d is the length \n", strlen(hash_input));
hash_input[strlen(hash_input)]=NULL;
}
if (argc > 1) {
// MDFile (digest,argv[1]);
// MDString (digest, file_buffer);
digest = (unsigned char *)nsMsgMD5Digest(hash_input, strlen(hash_input));
printf("%s is the digest \n", digest);
for (index =0; index <16;++index)
{
strcpy(&(final_hash[3*index])," ");
num=digest[index];
// printf("the num is %d and the dig is %s\n", num,&(digest[index]));
sprintf(&(final_hash[(3*index)+1]),"%0.2x",num);
// printf ("inside the for %s and the index %d \n", &(final_hash[3*index]), index);
}
final_hash[48]=NULL;
// printf("the hashed output is %s\n", final_hash);
strncpy (final_buf, "//",2);
final_buf[2]=NULL;
// printf ("the final hex %0.2x \n", "b");
strncat(final_buf,final_hash,48);
// printf ("the final buf %s\n",final_buf);
final_buf[50]=NULL;
printf ("%s is the final buffer \n",final_buf);
MDPrint (outputfile, file_buffer, final_buf,f_size);
} else {
printf("Usage: md5 <file> \n");
}
//free(file_buffer);
return (0);
}
// To convert to Hex String
/*void HexConvert(digest, final_hash)
{
char *tuple;
char *map ="000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f202122232425262728292a2b2c2d2e2f303132333435363738393a3b3c3d3e3f404142434445464748494a4b4c4d4e4f505152535455565758595a5b5c5d5e5f606162636465666768696a6b6c6d6e6f707172737475767778797a7b7c7d7e7f808182838485868788898a8b8c8d8e8f909192939495969798999a9b9c9d9e9fa0a1a2a3a4a5a6a7a8a9aaabacadaeafb0b1b2b3b4b5b6b7b8b9babbbcbdbebfc0c1c2c3c4c5c6c7c8c9cacbcccdcecfd0d1d2d3d4d5d6d7d8d9dadbdcdddedfe0e1e2e3e4e5e6e7e8e9eaebecedeeeff0f1f2f3f4f5f6f7f8f9fafbfcfdfeff";
char *output = final_hash;
int index=0;
for (index =0; index <16;++index)
{
char *tuple =map[digest[index]];
*output++ = *tuple++;
*output++ = *tuple++;
}
*output ='\0';
}
*/
// Digests a file and prints the result.
/*static void MDFile (digest, filename)
unsigned char *digest;
char *filename;
{
FILE *file;
MD_CTX context;
int len;
unsigned char buffer[1024];
unsigned char magic_key[] = "VonGloda5652TX75235ISBN\0";
unsigned int key_len =strlen (magic_key);
if ((file = fopen (filename, "rb")) == NULL)
printf ("%s can't be opened\n", filename);
else {
MDInit (&context);
MDUpdate (&context, magic_key, key_len);
while (len = fread (buffer, 1, 1024, file))
MDUpdate (&context, buffer, len);
MDFinal (digest, &context);
fclose (file);
}
}
*/
// Digests a string and prints the result.
/*
static void MDString (digest, str)
unsigned char *digest;
char *str;
{
MD_CTX context;
unsigned int len = strlen (str);
unsigned char *magic_key = "VonGloda5652TX75235ISBN";
unsigned int key_len =(strlen (magic_key)+1);
MDInit (&context);
MDUpdate (&context, magic_key, key_len);
MDUpdate (&context, str, len);
MDFinal (digest, &context);
}
*/
void obscure (input, obscured, len)
const char *input;
char *obscured;
int len;
{
int i;
for (i = 0; i < len; i++) {
obscured[i] = (input[i] + OBSCURE_CODE) ;
}
obscured[len] = '\0';
}
/* Prints a message digest in hexadecimal or binary.
*/
static void MDPrint (outfile, file_buffer, final_buf, f_size)
char *outfile;
char *file_buffer;
unsigned char *final_buf;
//long file_size;
{
FILE *outp;
int len;
unsigned char buffer[1024];
char obscured[2000];
//printf("inside the mdprint \n");
if ((outp = fopen (outfile, "wb")) == NULL) {
printf ("%s can't be opened for writing\n", outfile);
} else {
if (bflag) {
// print in obscured digest
obscure(final_buf, obscured, 50);
printf ("finished first obscure\n");
fprintf(outp, "%s", obscured);
printf("%s is the 1 obscured \n",obscured);
// print in obscured end of file
obscure("\n", obscured, 1);
fprintf(outp, "%s", obscured);
printf("%s is the 2 obscured \n",obscured);
//print in obscured file
obscure(file_buffer, obscured, f_size);
fprintf(outp, "%s",obscured);
// printf ("the digest length is %ld now \n",strlen(file_buffer));
printf("%s is the 3 obscured \n",obscured);
} else {/*
// print in hex
obscure(digest, obscured, MD5_LENGTH);
fprintf(outp, "%s\n", obscured);
// for (i = 0; i < MD5_LENGTH; i++) {
// fprintf (outp, "%02x ", digest[i]);
// }
//
// print in obscured digest
obscure("\n", obscured, 1);
fprintf(outp, "%s\n", obscured);
while(len = fread (buffer, 1, 1024, inpp)) {
obscure(buffer, obscured, 1024);
fprintf(outp, "%s", obscured);
}*/
}
fclose (outp);
// fclose (inpp);
}
}

Просмотреть файл

@ -0,0 +1,256 @@
/* -*- 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.0 (the "NPL"); you may not use this file except in
* compliance with the NPL. You may obtain a copy of the NPL at
* http://www.mozilla.org/NPL/
*
* Software distributed under the NPL is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL
* for the specific language governing rights and limitations under the
* NPL.
*
* The Initial Developer of this code under the NPL is Netscape
* Communications Corporation. Portions created by Netscape are
* Copyright (C) 1998 Netscape Communications Corporation. All Rights
* Reserved.
*/
/*
* 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<nwords; i += 16)
{
for (j=0; j<16; j++)
{
MD5_MSGWORD(x[j],k);
++k;
}
MD5_WORD AA=A, BB=B, CC=C, DD=D;
#define ROUND1(a,b,c,d,k,s,i) \
a = b + MD5_ROL((a + F(b,c,d) + x[k] + T[i]),s)
ROUND1(A,B,C,D,0,7,0);
ROUND1(D,A,B,C,1,12,1);
ROUND1(C,D,A,B,2,17,2);
ROUND1(B,C,D,A,3,22,3);
ROUND1(A,B,C,D,4,7,4);
ROUND1(D,A,B,C,5,12,5);
ROUND1(C,D,A,B,6,17,6);
ROUND1(B,C,D,A,7,22,7);
ROUND1(A,B,C,D,8,7,8);
ROUND1(D,A,B,C,9,12,9);
ROUND1(C,D,A,B,10,17,10);
ROUND1(B,C,D,A,11,22,11);
ROUND1(A,B,C,D,12,7,12);
ROUND1(D,A,B,C,13,12,13);
ROUND1(C,D,A,B,14,17,14);
ROUND1(B,C,D,A,15,22,15);
#define ROUND2(a,b,c,d,k,s,i) \
a = b + MD5_ROL((a + G(b,c,d) + x[k] + T[i]),s)
ROUND2(A,B,C,D,1,5,16);
ROUND2(D,A,B,C,6,9,17);
ROUND2(C,D,A,B,11,14,18);
ROUND2(B,C,D,A,0,20,19);
ROUND2(A,B,C,D,5,5,20);
ROUND2(D,A,B,C,10,9,21);
ROUND2(C,D,A,B,15,14,22);
ROUND2(B,C,D,A,4,20,23);
ROUND2(A,B,C,D,9,5,24);
ROUND2(D,A,B,C,14,9,25);
ROUND2(C,D,A,B,3,14,26);
ROUND2(B,C,D,A,8,20,27);
ROUND2(A,B,C,D,13,5,28);
ROUND2(D,A,B,C,2,9,29);
ROUND2(C,D,A,B,7,14,30);
ROUND2(B,C,D,A,12,20,31);
#define ROUND3(a,b,c,d,k,s,i) \
a = b + MD5_ROL((a + H(b,c,d) + x[k] + T[i]),s)
ROUND3(A,B,C,D,5,4,32);
ROUND3(D,A,B,C,8,11,33);
ROUND3(C,D,A,B,11,16,34);
ROUND3(B,C,D,A,14,23,35);
ROUND3(A,B,C,D,1,4,36);
ROUND3(D,A,B,C,4,11,37);
ROUND3(C,D,A,B,7,16,38);
ROUND3(B,C,D,A,10,23,39);
ROUND3(A,B,C,D,13,4,40);
ROUND3(D,A,B,C,0,11,41);
ROUND3(C,D,A,B,3,16,42);
ROUND3(B,C,D,A,6,23,43);
ROUND3(A,B,C,D,9,4,44);
ROUND3(D,A,B,C,12,11,45);
ROUND3(C,D,A,B,15,16,46);
ROUND3(B,C,D,A,2,23,47);
#define ROUND4(a,b,c,d,k,s,i) \
a = b + MD5_ROL((a + I(b,c,d) + x[k] + T[i]),s)
ROUND4(A,B,C,D,0,6,48);
ROUND4(D,A,B,C,7,10,49);
ROUND4(C,D,A,B,14,15,50);
ROUND4(B,C,D,A,5,21,51);
ROUND4(A,B,C,D,12,6,52);
ROUND4(D,A,B,C,3,10,53);
ROUND4(C,D,A,B,10,15,54);
ROUND4(B,C,D,A,1,21,55);
ROUND4(A,B,C,D,8,6,56);
ROUND4(D,A,B,C,15,10,57);
ROUND4(C,D,A,B,6,15,58);
ROUND4(B,C,D,A,13,21,59);
ROUND4(A,B,C,D,4,6,60);
ROUND4(D,A,B,C,11,10,61);
ROUND4(C,D,A,B,2,15,62);
ROUND4(B,C,D,A,9,21,63);
A += AA;
B += BB;
C += CC;
D += DD;
}
union md5_endian ea, eb, ec, ed;
ea.m_word=A;
eb.m_word=B;
ec.m_word=C;
ed.m_word=D;
m_digest[0]=ea.m_bytes.m_0;
m_digest[1]=ea.m_bytes.m_1;
m_digest[2]=ea.m_bytes.m_2;
m_digest[3]=ea.m_bytes.m_3;
m_digest[4]=eb.m_bytes.m_0;
m_digest[5]=eb.m_bytes.m_1;
m_digest[6]=eb.m_bytes.m_2;
m_digest[7]=eb.m_bytes.m_3;
m_digest[8]=ec.m_bytes.m_0;
m_digest[9]=ec.m_bytes.m_1;
m_digest[10]=ec.m_bytes.m_2;
m_digest[11]=ec.m_bytes.m_3;
m_digest[12]=ed.m_bytes.m_0;
m_digest[13]=ed.m_bytes.m_1;
m_digest[14]=ed.m_bytes.m_2;
m_digest[15]=ed.m_bytes.m_3;
return (m_digest);
}

Просмотреть файл

@ -0,0 +1,41 @@
/* -*- 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.0 (the "NPL"); you may not use this file except in
* compliance with the NPL. You may obtain a copy of the NPL at
* http://www.mozilla.org/NPL/
*
* Software distributed under the NPL is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL
* for the specific language governing rights and limitations under the
* NPL.
*
* The Initial Developer of this code under the NPL is Netscape
* Communications Corporation. Portions created by Netscape are
* Copyright (C) 1998 Netscape Communications Corporation. All Rights
* Reserved.
*/
/*
* MD5 digest implementation
*
* contributed by sam@email-scan.webcircle.com
*/
//#ifndef __nsMsgMD5_h
//#define __nsMsgMD5_h
//#include "nscore.h"
//NS_BEGIN_EXTERN_C
//
// RFC 1321 MD5 Message digest calculation.
//
// Returns a pointer to a sixteen-byte message digest.
//
const void *nsMsgMD5Digest(const void *msg, unsigned int len);
//NS_END_EXTERN_C
//#endif