⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 rvsha1.c

📁 h.248协议源码
💻 C
字号:
/******************************************************************************
Filename:    rvsha1.c
Description: sha1 hashing and authentication algorithms
*******************************************************************************
                Copyright (c) 2000 RADVision Inc.
*******************************************************************************
NOTICE:
This document contains information that is proprietary to RADVision Inc.
No part of this publication may be reproduced in any form whatsoever without
written prior approval by RADVision Inc.

RADVision Inc. reserves the right to revise this publication and make changes
without obligation to notify any person of such revisions or changes.
******************************************************************************/

#include <string.h>
#include "rvmem.h"
#include "rvsecutil.h"
#include "rvsha1.h"

static void rvSha1Loop(const char *buf, size_t buflen, char *output)
{
	const size_t numBlocks = buflen / 64;
	RvUint32 h0 = 0x67452301, h1 = 0xefcdab89, h2 = 0x98badcfe, h3 = 0x10325476, h4 = 0xc3d2e1f0;
	RvUint32 W[80];
	RvUint32 *writeWord = (RvUint32 *)output;
	size_t i;

	for(i=0; i<numBlocks; ++i)
	{
		static const RvUint32 K[] = { 0x5a827999, 0x6ed9eba1, 0x8f1bbcdc, 0xca62c1d6 };
		static const RvSecAuxFunc func[] = { rvSecAuxFuncF, rvSecAuxFuncH, rvSecAuxFuncJ, rvSecAuxFuncH };
		const RvUint32 *M = (const RvUint32 *)(buf + 64 * i);
		RvUint32 a=h0, b=h1, c=h2, d=h3, e=h4;
		size_t t;
		for(t=0; t<16; ++t)
			W[t] = rvToBigEndian(M[t]);
		for(; t<80; ++t)
			W[t] = rvLeftRotate(W[t-3] ^ W[t-8] ^ W[t-14] ^ W[t-16], 1);
		for(t=0; t<80; ++t)
		{
			size_t round = t / 20;
			RvUint32 temp = rvLeftRotate(a, 5) + func[round](b, c, d) + e + W[t] + K[round];
			e = d, d = c, c = rvLeftRotate(b, 30), b = a, a = temp;
		}
		h0 += a, h1 += b, h2 += c, h3 += d, h4 += e;
	}
	*writeWord++ = rvToBigEndian(h0);
	*writeWord++ = rvToBigEndian(h1);
	*writeWord++ = rvToBigEndian(h2);
	*writeWord++ = rvToBigEndian(h3);
	*writeWord++ = rvToBigEndian(h4);
}

static void rvSha1WithBuffer(char *buf, size_t textlen, size_t buflen, char *output)
{
	/* pad with a single 1 bit followed by all 0 bits */
	memset(buf + textlen, 0, buflen - textlen);
	buf[textlen] = (char)0x80;

	/* append length in bits as 64 bit big endian integer */
	*(RvUint32*)(buf + buflen - 4) = rvToBigEndian(8 * textlen);

	rvSha1Loop(buf, buflen, output);
}

/*$
{function:
	{name: rvSha1 }
	{superpackage: Security }
	{include: rvsha1.h}
	{description:
		{p: This method performs the Secure Hash Algorithm 1(SHA-1) on a buffer to 
            produce a 160-bit message digest.}
	}
	{proto: void rvSha1(const char* text, size_t textlen, char* output);}
	{params:
		{param: {n: text}    {d:The text buffer to be hashed.}}
		{param: {n: textlen} {d:The length of the text buffer in bytes.}}
		{param: {n: output}  {d:The buffer to be filled in with the results. This buffer
                                should be at least RV_SHA1_HASHLENGTH (20) bytes long.}}
	}
}
$*/
void rvSha1(const char *text, size_t textlen, char *output)
{
	size_t buflen = textlen + rvSecGetPadding(textlen);
	char *buf = (char *)rvMemAlloc(buflen);
	memcpy(buf, text, textlen);
	rvSha1WithBuffer(buf, textlen, buflen, output);
	rvMemFree(buf);
}

/*$
{function:
	{name: rvHmacSha1 }
	{superpackage: Security}	
	{include: rvsha1.h}
	{description:
		{p: This method creates a Hashed Message Authentication Code (HMAC) using the
            SHA-1 algorithm.}
		{p: See the IETF Informational RFC 2104 "HMAC: Keyed-Hashing for message
            authentication" for information on HMAC.}
	}
	{proto: void rvHmacSha1(const char* text, size_t textlen, const char* key, size_t keylen, char* output);}
	{params:
		{param: {n: text}    {d:The text buffer to be hashed.}}
		{param: {n: textlen} {d:The length of the text buffer in bytes.}}
		{param: {n: key}     {d:The key to use in the hash. }}
		{param: {n: keylen}  {d:The length of the key in bytes.}}
		{param: {n: output}  {d:The buffer to be filled in with the results. This buffer
                                should be at least RV_SHA1_HASHLENGTH (20) bytes long.}}
	}
}
$*/
void rvHmacSha1(const char *text, size_t textlen, const char *key, size_t keylen, char *output)
{
	rvHmac(text, textlen, key, keylen, output, rvSha1, rvSha1WithBuffer, RV_SHA1_HASHLENGTH);
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -