[BACK]Return to ntlsha.cpp CVS log [TXT][DIR] Up to [local] / OpenXM / src / ox_ntl / crypt / sha1

File: [local] / OpenXM / src / ox_ntl / crypt / sha1 / ntlsha.cpp (download)

Revision 1.3, Sun May 16 15:02:39 2004 UTC (20 years ago) by iwane
Branch: MAIN
CVS Tags: R_1_3_1-2, RELEASE_1_3_1_13b, RELEASE_1_2_3_12, RELEASE_1_2_3, KNOPPIX_2006, HEAD, DEB_REL_1_2_3-9
Changes since 1.2: +4 -8 lines

fixed a bug.
added sample code.

/* $OpenXM: OpenXM/src/ox_ntl/crypt/sha1/ntlsha.cpp,v 1.3 2004/05/16 15:02:39 iwane Exp $ */
/* RFC 3174 -- see sha1.c */

#include <NTL/ZZ.h>

#include <stdlib.h>
#include <stdio.h>

#include "sha1.h"

int
ntl_sha1_h(ZZ &sha, const ZZ &m, unsigned int *t)
{
	unsigned char h[20]; // 160 bit
	unsigned char *msg, msg_buf[1024];
	unsigned int msglen = sizeof(msg);
	unsigned int i;
	unsigned char uch;
	int ret;

	msglen = NumBytes(m);
	if (msglen <= sizeof(msg_buf)) {
		msg = msg_buf;
	} else {
		msg = new unsigned char[msglen];
	}

	BytesFromZZ(msg, m, msglen);

	for (i = 0; i < msglen / 2; i++) {
		uch = msg[i];
		msg[i] = msg[msglen - i - 1];
		msg[msglen - i - 1] = uch;
	}

	ret = sha1_h(h, msg, msglen, t);

	if (msg != msg_buf)
		delete [] msg;

	for (i = 0; i < sizeof(h) / 2; i++) {
		uch = h[i];
		h[i] = h[sizeof(h) - i - 1];
		h[sizeof(h) - i - 1] = uch;
	}

	ZZFromBytes(sha, h, sizeof(h));

	return (ret);
}

int
ntl_sha1(ZZ &sha, const ZZ &m)
{
	return (ntl_sha1_h(sha, m, NULL));
}