mirror of
https://github.com/kvazar-network/kevacoin.git
synced 2025-01-23 13:24:18 +00:00
Move {Read,Write}{LE,BE}{32,64} to common.h and use builtins if possible
This commit is contained in:
parent
a5bc9c0917
commit
7ecd9739d9
@ -75,6 +75,7 @@ BITCOIN_CORE_H = \
|
|||||||
rpcserver.h \
|
rpcserver.h \
|
||||||
script.h \
|
script.h \
|
||||||
serialize.h \
|
serialize.h \
|
||||||
|
crypto/common.h \
|
||||||
crypto/sha2.h \
|
crypto/sha2.h \
|
||||||
crypto/sha1.h \
|
crypto/sha1.h \
|
||||||
crypto/ripemd160.h \
|
crypto/ripemd160.h \
|
||||||
|
46
src/crypto/common.h
Normal file
46
src/crypto/common.h
Normal file
@ -0,0 +1,46 @@
|
|||||||
|
// Copyright (c) 2014 The Bitcoin developers
|
||||||
|
// Distributed under the MIT/X11 software license, see the accompanying
|
||||||
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
||||||
|
|
||||||
|
#ifndef BITCOIN_CRYPTO_COMMON_H
|
||||||
|
#define BITCOIN_CRYPTO_COMMON_H
|
||||||
|
|
||||||
|
#include <stdint.h>
|
||||||
|
|
||||||
|
#ifdef WIN32
|
||||||
|
uint32_t static inline ReadLE32(const unsigned char *ptr) { return *((uint32_t*)ptr); }
|
||||||
|
uint64_t static inline ReadLE64(const unsigned char *ptr) { return *((uint64_t*)ptr); }
|
||||||
|
|
||||||
|
void static inline WriteLE32(unsigned char *ptr, uint32_t x) { *((uint32_t*)ptr) = x; }
|
||||||
|
void static inline WriteLE64(unsigned char *ptr, uint64_t x) { *((uint64_t*)ptr) = x; }
|
||||||
|
|
||||||
|
uint32_t static inline ReadBE32(const unsigned char *ptr) {
|
||||||
|
return ((uint32_t)ptr[0] << 24 | (uint32_t)ptr[1] << 16 | (uint32_t)ptr[2] << 8 | (uint32_t)ptr[3]);
|
||||||
|
}
|
||||||
|
|
||||||
|
uint64_t static inline ReadBE64(const unsigned char *ptr) {
|
||||||
|
return ((uint64_t)ptr[0] << 56 | (uint64_t)ptr[1] << 48 | (uint64_t)ptr[2] << 40 | (uint64_t)ptr[3] << 32 |
|
||||||
|
(uint64_t)ptr[4] << 24 | (uint64_t)ptr[5] << 16 | (uint64_t)ptr[6] << 8 | (uint64_t)ptr[7]);
|
||||||
|
}
|
||||||
|
|
||||||
|
void static inline WriteBE32(unsigned char *ptr, uint32_t x) {
|
||||||
|
ptr[0] = x >> 24; ptr[1] = x >> 16; ptr[2] = x >> 8; ptr[3] = x;
|
||||||
|
}
|
||||||
|
|
||||||
|
void static inline WriteBE64(unsigned char *ptr, uint64_t x) {
|
||||||
|
ptr[0] = x >> 56; ptr[1] = x >> 48; ptr[2] = x >> 40; ptr[3] = x >> 32;
|
||||||
|
ptr[4] = x >> 24; ptr[5] = x >> 16; ptr[6] = x >> 8; ptr[7] = x;
|
||||||
|
}
|
||||||
|
#else
|
||||||
|
# include <endian.h>
|
||||||
|
uint32_t static inline ReadLE32(const unsigned char *ptr) { return le32toh(*((uint32_t*)ptr)); }
|
||||||
|
uint64_t static inline ReadLE64(const unsigned char *ptr) { return le64toh(*((uint64_t*)ptr)); }
|
||||||
|
void static inline WriteLE32(unsigned char *ptr, uint32_t x) { *((uint32_t*)ptr) = htole32(x); }
|
||||||
|
void static inline WriteLE64(unsigned char *ptr, uint64_t x) { *((uint64_t*)ptr) = htole64(x); }
|
||||||
|
|
||||||
|
uint32_t static inline ReadBE32(const unsigned char *ptr) { return be32toh(*((uint32_t*)ptr)); }
|
||||||
|
uint64_t static inline ReadBE64(const unsigned char *ptr) { return be64toh(*((uint64_t*)ptr)); }
|
||||||
|
void static inline WriteBE32(unsigned char *ptr, uint32_t x) { *((uint32_t*)ptr) = htobe32(x); }
|
||||||
|
void static inline WriteBE64(unsigned char *ptr, uint64_t x) { *((uint64_t*)ptr) = htobe64(x); }
|
||||||
|
#endif
|
||||||
|
#endif
|
@ -4,24 +4,12 @@
|
|||||||
|
|
||||||
#include "crypto/ripemd160.h"
|
#include "crypto/ripemd160.h"
|
||||||
|
|
||||||
|
#include "crypto/common.h"
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
// Internal implementation code.
|
// Internal implementation code.
|
||||||
namespace {
|
namespace {
|
||||||
|
|
||||||
/** Read 4 bytes, and interpret them as a 32-bit unsigned little-endian integer. */
|
|
||||||
uint32_t inline ReadLE32(const unsigned char *data) {
|
|
||||||
return ((uint32_t)data[0] | (uint32_t)data[1] << 8 | (uint32_t)data[2] << 16 | (uint32_t)data[3] << 24);
|
|
||||||
}
|
|
||||||
|
|
||||||
/** Write a 32-bit unsigned little-endian integer. */
|
|
||||||
void inline WriteLE32(unsigned char *data, uint32_t x) {
|
|
||||||
data[0] = x;
|
|
||||||
data[1] = x >> 8;
|
|
||||||
data[2] = x >> 16;
|
|
||||||
data[3] = x >> 24;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Internal RIPEMD-160 implementation.
|
/// Internal RIPEMD-160 implementation.
|
||||||
namespace ripemd160 {
|
namespace ripemd160 {
|
||||||
|
|
||||||
@ -199,8 +187,7 @@ CRIPEMD160& CRIPEMD160::Write(const unsigned char *data, size_t len) {
|
|||||||
void CRIPEMD160::Finalize(unsigned char *hash) {
|
void CRIPEMD160::Finalize(unsigned char *hash) {
|
||||||
static const unsigned char pad[64] = {0x80};
|
static const unsigned char pad[64] = {0x80};
|
||||||
unsigned char sizedesc[8];
|
unsigned char sizedesc[8];
|
||||||
WriteLE32(sizedesc, bytes << 3);
|
WriteLE64(sizedesc, bytes << 3);
|
||||||
WriteLE32(sizedesc+4, bytes >> 29);
|
|
||||||
Write(pad, 1 + ((119 - (bytes % 64)) % 64));
|
Write(pad, 1 + ((119 - (bytes % 64)) % 64));
|
||||||
Write(sizedesc, 8);
|
Write(sizedesc, 8);
|
||||||
WriteLE32(hash, s[0]);
|
WriteLE32(hash, s[0]);
|
||||||
|
@ -4,24 +4,12 @@
|
|||||||
|
|
||||||
#include "crypto/sha1.h"
|
#include "crypto/sha1.h"
|
||||||
|
|
||||||
|
#include "crypto/common.h"
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
// Internal implementation code.
|
// Internal implementation code.
|
||||||
namespace {
|
namespace {
|
||||||
|
|
||||||
/** Read 4 bytes, and interpret them as a 32-bit unsigned big-endian integer. */
|
|
||||||
uint32_t inline ReadBE32(const unsigned char *data) {
|
|
||||||
return ((uint32_t)data[0] << 24 | (uint32_t)data[1] << 16 | (uint32_t)data[2] << 8 | (uint32_t)data[3]);
|
|
||||||
}
|
|
||||||
|
|
||||||
/** Write a 32-bit unsigned big-endian integer. */
|
|
||||||
void inline WriteBE32(unsigned char *data, uint32_t x) {
|
|
||||||
data[0] = x >> 24;
|
|
||||||
data[1] = x >> 16;
|
|
||||||
data[2] = x >> 8;
|
|
||||||
data[3] = x;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Internal SHA-1 implementation.
|
/// Internal SHA-1 implementation.
|
||||||
namespace sha1 {
|
namespace sha1 {
|
||||||
|
|
||||||
@ -187,8 +175,7 @@ CSHA1& CSHA1::Write(const unsigned char *data, size_t len) {
|
|||||||
void CSHA1::Finalize(unsigned char *hash) {
|
void CSHA1::Finalize(unsigned char *hash) {
|
||||||
static const unsigned char pad[64] = {0x80};
|
static const unsigned char pad[64] = {0x80};
|
||||||
unsigned char sizedesc[8];
|
unsigned char sizedesc[8];
|
||||||
WriteBE32(sizedesc, bytes >> 29);
|
WriteBE64(sizedesc, bytes << 3);
|
||||||
WriteBE32(sizedesc+4, bytes << 3);
|
|
||||||
Write(pad, 1 + ((119 - (bytes % 64)) % 64));
|
Write(pad, 1 + ((119 - (bytes % 64)) % 64));
|
||||||
Write(sizedesc, 8);
|
Write(sizedesc, 8);
|
||||||
WriteBE32(hash, s[0]);
|
WriteBE32(hash, s[0]);
|
||||||
|
@ -4,42 +4,12 @@
|
|||||||
|
|
||||||
#include "crypto/sha2.h"
|
#include "crypto/sha2.h"
|
||||||
|
|
||||||
|
#include "crypto/common.h"
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
// Internal implementation code.
|
// Internal implementation code.
|
||||||
namespace {
|
namespace {
|
||||||
|
|
||||||
/** Read 4 bytes, and interpret them as a 32-bit unsigned big-endian integer. */
|
|
||||||
uint32_t inline ReadBE32(const unsigned char *data) {
|
|
||||||
return ((uint32_t)data[0] << 24 | (uint32_t)data[1] << 16 | (uint32_t)data[2] << 8 | (uint32_t)data[3]);
|
|
||||||
}
|
|
||||||
|
|
||||||
/** Write a 32-bit unsigned big-endian integer. */
|
|
||||||
void inline WriteBE32(unsigned char *data, uint32_t x) {
|
|
||||||
data[0] = x >> 24;
|
|
||||||
data[1] = x >> 16;
|
|
||||||
data[2] = x >> 8;
|
|
||||||
data[3] = x;
|
|
||||||
}
|
|
||||||
|
|
||||||
/** Read 8 bytes, and interpret them as a 64-bit unsigned big-endian integer. */
|
|
||||||
uint64_t inline ReadBE64(const unsigned char *data) {
|
|
||||||
return ((uint64_t)data[0] << 56 | (uint64_t)data[1] << 48 | (uint64_t)data[2] << 40 | (uint64_t)data[3] << 32 |
|
|
||||||
(uint64_t)data[4] << 24 | (uint64_t)data[5] << 16 | (uint64_t)data[6] << 8 | (uint64_t)data[7]);
|
|
||||||
}
|
|
||||||
|
|
||||||
/** Write a 64-bit unsigned big-endian integer. */
|
|
||||||
void inline WriteBE64(unsigned char *data, uint64_t x) {
|
|
||||||
data[0] = x >> 56;
|
|
||||||
data[1] = x >> 48;
|
|
||||||
data[2] = x >> 40;
|
|
||||||
data[3] = x >> 32;
|
|
||||||
data[4] = x >> 24;
|
|
||||||
data[5] = x >> 16;
|
|
||||||
data[6] = x >> 8;
|
|
||||||
data[7] = x;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Internal SHA-256 implementation.
|
/// Internal SHA-256 implementation.
|
||||||
namespace sha256 {
|
namespace sha256 {
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user