made sha1.cpp build in sunpro

This commit is contained in:
Arvid Norberg 2009-08-08 15:15:27 +00:00
parent 7933132cd9
commit 98ece8c1ca
1 changed files with 28 additions and 26 deletions

View File

@ -18,30 +18,32 @@ changelog at the end of the file.
// if you don't want boost // if you don't want boost
// replace with // replace with
// #include <stdint.h> // #include <stdint.h>
// typedef uint32_t u32;
// typedef uint8_t u8;
#include <boost/cstdint.hpp> #include <boost/cstdint.hpp>
using boost::uint32_t; typedef boost::uint32_t u32;
using boost::uint8_t; typedef boost::uint8_t u8;
#include "libtorrent/config.hpp" #include "libtorrent/config.hpp"
struct TORRENT_EXPORT SHA_CTX struct TORRENT_EXPORT SHA_CTX
{ {
uint32_t state[5]; u32 state[5];
uint32_t count[2]; u32 count[2];
uint8_t buffer[64]; u8 buffer[64];
}; };
TORRENT_EXPORT void SHA1_Init(SHA_CTX* context); TORRENT_EXPORT void SHA1_Init(SHA_CTX* context);
TORRENT_EXPORT void SHA1_Update(SHA_CTX* context, uint8_t const* data, uint32_t len); TORRENT_EXPORT void SHA1_Update(SHA_CTX* context, u8 const* data, u32 len);
TORRENT_EXPORT void SHA1_Final(uint8_t* digest, SHA_CTX* context); TORRENT_EXPORT void SHA1_Final(u8* digest, SHA_CTX* context);
namespace namespace
{ {
union CHAR64LONG16 union CHAR64LONG16
{ {
uint8_t c[64]; u8 c[64];
uint32_t l[16]; u32 l[16];
}; };
#define rol(value, bits) (((value) << (bits)) | ((value) >> (32 - (bits)))) #define rol(value, bits) (((value) << (bits)) | ((value) >> (32 - (bits))))
@ -50,7 +52,7 @@ namespace
// I got the idea of expanding during the round function from SSLeay // I got the idea of expanding during the round function from SSLeay
struct little_endian_blk0 struct little_endian_blk0
{ {
static uint32_t apply(CHAR64LONG16* block, int i) static u32 apply(CHAR64LONG16* block, int i)
{ {
return block->l[i] = (rol(block->l[i],24)&0xFF00FF00) return block->l[i] = (rol(block->l[i],24)&0xFF00FF00)
| (rol(block->l[i],8)&0x00FF00FF); | (rol(block->l[i],8)&0x00FF00FF);
@ -59,7 +61,7 @@ namespace
struct big_endian_blk0 struct big_endian_blk0
{ {
static uint32_t apply(CHAR64LONG16* block, int i) static u32 apply(CHAR64LONG16* block, int i)
{ {
return block->l[i]; return block->l[i];
} }
@ -78,13 +80,13 @@ namespace
// Hash a single 512-bit block. This is the core of the algorithm. // Hash a single 512-bit block. This is the core of the algorithm.
template <class BlkFun> template <class BlkFun>
void SHA1Transform(uint32_t state[5], uint8_t const buffer[64]) void SHA1Transform(u32 state[5], u8 const buffer[64])
{ {
using namespace std; using namespace std;
uint32_t a, b, c, d, e; u32 a, b, c, d, e;
CHAR64LONG16* block; CHAR64LONG16* block;
uint8_t workspace[64]; u8 workspace[64];
block = (CHAR64LONG16*)workspace; block = (CHAR64LONG16*)workspace;
memcpy(block, buffer, 64); memcpy(block, buffer, 64);
@ -136,10 +138,10 @@ namespace
} }
template <class BlkFun> template <class BlkFun>
void internal_update(SHA_CTX* context, uint8_t const* data, uint32_t len) void internal_update(SHA_CTX* context, u8 const* data, u32 len)
{ {
using namespace std; using namespace std;
uint32_t i, j; // JHB u32 i, j; // JHB
#ifdef VERBOSE #ifdef VERBOSE
SHAPrintContext(context, "before"); SHAPrintContext(context, "before");
@ -169,8 +171,8 @@ namespace
bool is_big_endian() bool is_big_endian()
{ {
uint32_t test = 1; u32 test = 1;
return *reinterpret_cast<uint8_t*>(&test) == 0; return *reinterpret_cast<u8*>(&test) == 0;
} }
} }
@ -190,7 +192,7 @@ void SHA1_Init(SHA_CTX* context)
// Run your data through this. // Run your data through this.
void SHA1_Update(SHA_CTX* context, uint8_t const* data, uint32_t len) void SHA1_Update(SHA_CTX* context, u8 const* data, u32 len)
{ {
#if defined __BIG_ENDIAN__ #if defined __BIG_ENDIAN__
internal_update<big_endian_blk0>(context, data, len); internal_update<big_endian_blk0>(context, data, len);
@ -209,24 +211,24 @@ void SHA1_Update(SHA_CTX* context, uint8_t const* data, uint32_t len)
// Add padding and return the message digest. // Add padding and return the message digest.
void SHA1_Final(uint8_t* digest, SHA_CTX* context) void SHA1_Final(u8* digest, SHA_CTX* context)
{ {
uint8_t finalcount[8]; u8 finalcount[8];
for (uint32_t i = 0; i < 8; ++i) for (u32 i = 0; i < 8; ++i)
{ {
// Endian independent // Endian independent
finalcount[i] = static_cast<uint8_t>( finalcount[i] = static_cast<u8>(
(context->count[(i >= 4 ? 0 : 1)] (context->count[(i >= 4 ? 0 : 1)]
>> ((3-(i & 3)) * 8) ) & 255); >> ((3-(i & 3)) * 8) ) & 255);
} }
SHA1_Update(context, (uint8_t const*)"\200", 1); SHA1_Update(context, (u8 const*)"\200", 1);
while ((context->count[0] & 504) != 448) while ((context->count[0] & 504) != 448)
SHA1_Update(context, (uint8_t const*)"\0", 1); SHA1_Update(context, (u8 const*)"\0", 1);
SHA1_Update(context, finalcount, 8); // Should cause a SHA1Transform() SHA1_Update(context, finalcount, 8); // Should cause a SHA1Transform()
for (uint32_t i = 0; i < 20; ++i) for (u32 i = 0; i < 20; ++i)
{ {
digest[i] = static_cast<unsigned char>( digest[i] = static_cast<unsigned char>(
(context->state[i>>2] >> ((3-(i & 3)) * 8) ) & 255); (context->state[i>>2] >> ((3-(i & 3)) * 8) ) & 255);