From df9c15d40a50ed17baa7bdbb11ef6cca9d410cf3 Mon Sep 17 00:00:00 2001 From: zyp Date: Mon, 8 May 2006 13:23:20 +0000 Subject: [project @ zyp-20060508132320-2c118d1a7e2505db] [project @ 24] Converted from make to distutils. --- Makefile | 27 ---------------- __init__.py | 11 ------- crc32.cpp | 41 ------------------------- crc32.h | 11 ------- ed2k.cpp | 41 ------------------------- ed2k.h | 17 ----------- hash.cpp | 85 --------------------------------------------------- hash.h | 37 ---------------------- hash_wrapper.cpp | 14 --------- multihash/__init__.py | 11 +++++++ setup.py | 15 +++++++++ src/crc32.cpp | 41 +++++++++++++++++++++++++ src/crc32.h | 11 +++++++ src/ed2k.cpp | 41 +++++++++++++++++++++++++ src/ed2k.h | 17 +++++++++++ src/hash.cpp | 85 +++++++++++++++++++++++++++++++++++++++++++++++++++ src/hash.h | 37 ++++++++++++++++++++++ src/hash_wrapper.cpp | 14 +++++++++ 18 files changed, 272 insertions(+), 284 deletions(-) delete mode 100644 Makefile delete mode 100644 __init__.py delete mode 100644 crc32.cpp delete mode 100644 crc32.h delete mode 100644 ed2k.cpp delete mode 100644 ed2k.h delete mode 100644 hash.cpp delete mode 100644 hash.h delete mode 100644 hash_wrapper.cpp create mode 100644 multihash/__init__.py create mode 100644 setup.py create mode 100644 src/crc32.cpp create mode 100644 src/crc32.h create mode 100644 src/ed2k.cpp create mode 100644 src/ed2k.h create mode 100644 src/hash.cpp create mode 100644 src/hash.h create mode 100644 src/hash_wrapper.cpp diff --git a/Makefile b/Makefile deleted file mode 100644 index 0d897d0..0000000 --- a/Makefile +++ /dev/null @@ -1,27 +0,0 @@ -CC=gcc -CFLAGS= -CPP=g++ -CPPFLAGS= -LD=gcc -LDFLAGS=-shared - -OBJECTS=hash_wrapper.o hash.o crc32.o ed2k.o -TARGET=_hash.so -INCLUDE=-I /usr/include/python2.4/ -LIB=-l boost_python -l ssl - -all: $(TARGET) - -clean: - -rm $(TARGET) $(OBJECTS) - -# Mål-regel -$(TARGET): $(OBJECTS) Makefile - $(LD) $(LDFLAGS) -o $(TARGET) $(OBJECTS) $(LIB) - -# Pseudoregler -%.o: %.c Makefile - $(CC) $(CFLAGS) -o $@ -c $< $(INCLUDE) - -%.o: %.cpp Makefile - $(CPP) $(CPPFLAGS) -o $@ -c $< $(INCLUDE) \ No newline at end of file diff --git a/__init__.py b/__init__.py deleted file mode 100644 index 0333819..0000000 --- a/__init__.py +++ /dev/null @@ -1,11 +0,0 @@ -from _hash import * - -def file_hash(name): - h = Hash() - f = open(name) - data = f.read(32768) - while data: - h.update(data) - data = f.read(32768) - f.close() - return h \ No newline at end of file diff --git a/crc32.cpp b/crc32.cpp deleted file mode 100644 index 55d6dd2..0000000 --- a/crc32.cpp +++ /dev/null @@ -1,41 +0,0 @@ -#include "crc32.h" - -int* CRC32::crc_table; - -int* CRC32::generate_table() { - int crc; - int* table = new int[256]; - for(int i = 0; i < 256; i++) { - crc = i << 24; - for(int j = 0; j < 8; j++) { - if(crc & 0x80000000) { - crc = (crc << 1) ^ 0x04c11db7; - } else { - crc = crc << 1; - } - } - table[i] = crc; - } - return table; -} - -int CRC32::reflect(int data, int bits) { - int x = 0; - for(int i = 0; i < bits; i++) { - x = x << 1; - x |= data & 1; - data = data >> 1; - } - return x; -} - -int CRC32::crc32(int crc, const char* data, int length) { - crc = ~reflect(crc, 32); - if(!crc_table) { - crc_table = generate_table(); - } - for (int i = 0; i < length; i++) { - crc = (crc << 8) ^ crc_table[((crc >> 24) ^ reflect(data[i], 8)) & 0xff]; - } - return ~reflect(crc, 32); -} diff --git a/crc32.h b/crc32.h deleted file mode 100644 index 646ccce..0000000 --- a/crc32.h +++ /dev/null @@ -1,11 +0,0 @@ -#ifndef _CRC32_H_ -#define _CRC32_H_ - -namespace CRC32 { - extern int* crc_table; - int* generate_table(); - int reflect(int data, int bits); - int crc32(int crc, const char* data, int length); -} - -#endif // _CRC32_H_ diff --git a/ed2k.cpp b/ed2k.cpp deleted file mode 100644 index 92e7b15..0000000 --- a/ed2k.cpp +++ /dev/null @@ -1,41 +0,0 @@ -#include "ed2k.h" - -template -inline T min(T a, T b) { - return (a > b) ? b : a; -} - -Ed2k::Ed2k() { - MD4_Init(&md4_partial); - MD4_Init(&md4_final); - size_total = 0; -} - -void Ed2k::update(const char* data, int length) { - while(length) { - if(!(size_total % (9500 * 1024)) && size_total) { - unsigned char digest[16]; - MD4_Final(digest, &md4_partial); - MD4_Update(&md4_final, digest, 16); - MD4_Init(&md4_partial); - } - int size = min(length, (9500 * 1024) - (size_total % (9500 * 1024))); - MD4_Update(&md4_partial, data, size); - length -= size; - data += size; - size_total += size; - }; -} - -char* Ed2k::digest() { - char* digest = new char[16]; - if(size_total > (9500 * 1024)) { - unsigned char digest_partial[16]; - MD4_Final(digest_partial, &md4_partial); - MD4_Update(&md4_final, digest_partial, 16); - MD4_Final((unsigned char*)digest, &md4_final); - } else { - MD4_Final((unsigned char*)digest, &md4_partial); - } - return digest; -} diff --git a/ed2k.h b/ed2k.h deleted file mode 100644 index 1be7302..0000000 --- a/ed2k.h +++ /dev/null @@ -1,17 +0,0 @@ -#ifndef _ED2K_H_ -#define _ED2K_H_ - -#include - -class Ed2k { - private: - MD4_CTX md4_partial; - MD4_CTX md4_final; - unsigned int size_total; - public: - Ed2k(); - void update(const char* data, int length); - char* digest(); -}; - -#endif // _ED2K_H_ diff --git a/hash.cpp b/hash.cpp deleted file mode 100644 index d844c46..0000000 --- a/hash.cpp +++ /dev/null @@ -1,85 +0,0 @@ -#include "hash.h" -#include "crc32.h" - -#include - -namespace Hex { - static char* digits = "0123456789abcdef"; - std::string hex(char* bin, int length) { - std::string s(length * 2, ' '); - for(int i = 0; i < length; i++) { - s[i*2] = digits[(bin[i] >> 4) & 0xf]; - s[i*2+1] = digits[bin[i] & 0xf]; - } - return s; - } - std::string hex(int bin) { - std::string s(sizeof(int) * 2, ' '); - for(int i = 0; i < sizeof(int) * 2; i++) { - s[sizeof(int) * 2 - 1 - i] = digits[bin & 0xf]; - bin = bin >> 4; - } - return s; - } -} - -Hash::Hash() { - finished = false; - - crc32_ctx = 0; - crc32_str = ""; - - ed2k_str = ""; - - MD5_Init(&md5_ctx); - md5_str = ""; - - SHA1_Init(&sha1_ctx); - sha1_str = ""; -} - -void Hash::update(std::string data) { - if(finished) { - throw std::runtime_error("Can't update after digest."); - } - crc32_ctx = CRC32::crc32(crc32_ctx, data.c_str(), data.length()); - ed2k_ctx.update(data.c_str(), data.length()); - MD5_Update(&md5_ctx, data.c_str(), data.length()); - SHA1_Update(&sha1_ctx, data.c_str(), data.length()); -} - -std::string Hash::crc32() { - return Hex::hex(crc32_ctx); -} - -std::string Hash::ed2k() { - if(!ed2k_str.length()) { - finished = true; - char* digest = ed2k_ctx.digest(); - ed2k_str = Hex::hex(digest, 16); - delete digest; - } - return ed2k_str; -} - -std::string Hash::md5() { - if(!md5_str.length()) { - finished = true; - char* digest = new char[16]; - MD5_Final((unsigned char*)digest, &md5_ctx); - md5_str = Hex::hex(digest, 16); - delete digest; - } - return md5_str; -} - -std::string Hash::sha1() { - if(!sha1_str.length()) { - finished = true; - char* digest = new char[20]; - SHA1_Final((unsigned char*)digest, &sha1_ctx); - sha1_str = Hex::hex(digest, 20); - delete digest; - } - return sha1_str; -} diff --git a/hash.h b/hash.h deleted file mode 100644 index 3c7b9c9..0000000 --- a/hash.h +++ /dev/null @@ -1,37 +0,0 @@ -#ifndef _HASH_H_ -#define _HASH_H_ - -#include - -#include "ed2k.h" - -#include -#include -#include - -class Hash { - private: - bool finished; - - int crc32_ctx; - std::string crc32_str; - - Ed2k ed2k_ctx; - std::string ed2k_str; - - MD5_CTX md5_ctx; - std::string md5_str; - - SHA_CTX sha1_ctx; - std::string sha1_str; - - public: - Hash(); - void update(std::string data); - std::string crc32(); - std::string ed2k(); - std::string md5(); - std::string sha1(); -}; - -#endif // _HASH_H_ diff --git a/hash_wrapper.cpp b/hash_wrapper.cpp deleted file mode 100644 index a4173b9..0000000 --- a/hash_wrapper.cpp +++ /dev/null @@ -1,14 +0,0 @@ -#include "hash.h" - -#include -using namespace boost::python; - -BOOST_PYTHON_MODULE(_hash) -{ - class_("Hash") - .def("update", &Hash::update) - .def("crc32", &Hash::crc32) - .def("ed2k", &Hash::ed2k) - .def("md5", &Hash::md5) - .def("sha1", &Hash::sha1); -} diff --git a/multihash/__init__.py b/multihash/__init__.py new file mode 100644 index 0000000..5bcf4d5 --- /dev/null +++ b/multihash/__init__.py @@ -0,0 +1,11 @@ +from _multihash import * + +def file_hash(name): + h = Multihash() + f = open(name) + data = f.read(32768) + while data: + h.update(data) + data = f.read(32768) + f.close() + return h \ No newline at end of file diff --git a/setup.py b/setup.py new file mode 100644 index 0000000..cea2803 --- /dev/null +++ b/setup.py @@ -0,0 +1,15 @@ +#!/usr/bin/env python + +import os +from distutils.core import setup, Extension + +setup( + name='multihash', + packages = ['multihash'], + ext_modules=[ + Extension('multihash._multihash', + ['src/' + x for x in os.listdir('src') if x.endswith('.cpp')], + include_dirs = [], + library_dirs = [], + libraries = ["boost_python", "ssl"], + extra_compile_args = [])]) diff --git a/src/crc32.cpp b/src/crc32.cpp new file mode 100644 index 0000000..55d6dd2 --- /dev/null +++ b/src/crc32.cpp @@ -0,0 +1,41 @@ +#include "crc32.h" + +int* CRC32::crc_table; + +int* CRC32::generate_table() { + int crc; + int* table = new int[256]; + for(int i = 0; i < 256; i++) { + crc = i << 24; + for(int j = 0; j < 8; j++) { + if(crc & 0x80000000) { + crc = (crc << 1) ^ 0x04c11db7; + } else { + crc = crc << 1; + } + } + table[i] = crc; + } + return table; +} + +int CRC32::reflect(int data, int bits) { + int x = 0; + for(int i = 0; i < bits; i++) { + x = x << 1; + x |= data & 1; + data = data >> 1; + } + return x; +} + +int CRC32::crc32(int crc, const char* data, int length) { + crc = ~reflect(crc, 32); + if(!crc_table) { + crc_table = generate_table(); + } + for (int i = 0; i < length; i++) { + crc = (crc << 8) ^ crc_table[((crc >> 24) ^ reflect(data[i], 8)) & 0xff]; + } + return ~reflect(crc, 32); +} diff --git a/src/crc32.h b/src/crc32.h new file mode 100644 index 0000000..646ccce --- /dev/null +++ b/src/crc32.h @@ -0,0 +1,11 @@ +#ifndef _CRC32_H_ +#define _CRC32_H_ + +namespace CRC32 { + extern int* crc_table; + int* generate_table(); + int reflect(int data, int bits); + int crc32(int crc, const char* data, int length); +} + +#endif // _CRC32_H_ diff --git a/src/ed2k.cpp b/src/ed2k.cpp new file mode 100644 index 0000000..92e7b15 --- /dev/null +++ b/src/ed2k.cpp @@ -0,0 +1,41 @@ +#include "ed2k.h" + +template +inline T min(T a, T b) { + return (a > b) ? b : a; +} + +Ed2k::Ed2k() { + MD4_Init(&md4_partial); + MD4_Init(&md4_final); + size_total = 0; +} + +void Ed2k::update(const char* data, int length) { + while(length) { + if(!(size_total % (9500 * 1024)) && size_total) { + unsigned char digest[16]; + MD4_Final(digest, &md4_partial); + MD4_Update(&md4_final, digest, 16); + MD4_Init(&md4_partial); + } + int size = min(length, (9500 * 1024) - (size_total % (9500 * 1024))); + MD4_Update(&md4_partial, data, size); + length -= size; + data += size; + size_total += size; + }; +} + +char* Ed2k::digest() { + char* digest = new char[16]; + if(size_total > (9500 * 1024)) { + unsigned char digest_partial[16]; + MD4_Final(digest_partial, &md4_partial); + MD4_Update(&md4_final, digest_partial, 16); + MD4_Final((unsigned char*)digest, &md4_final); + } else { + MD4_Final((unsigned char*)digest, &md4_partial); + } + return digest; +} diff --git a/src/ed2k.h b/src/ed2k.h new file mode 100644 index 0000000..1be7302 --- /dev/null +++ b/src/ed2k.h @@ -0,0 +1,17 @@ +#ifndef _ED2K_H_ +#define _ED2K_H_ + +#include + +class Ed2k { + private: + MD4_CTX md4_partial; + MD4_CTX md4_final; + unsigned int size_total; + public: + Ed2k(); + void update(const char* data, int length); + char* digest(); +}; + +#endif // _ED2K_H_ diff --git a/src/hash.cpp b/src/hash.cpp new file mode 100644 index 0000000..d844c46 --- /dev/null +++ b/src/hash.cpp @@ -0,0 +1,85 @@ +#include "hash.h" +#include "crc32.h" + +#include + +namespace Hex { + static char* digits = "0123456789abcdef"; + std::string hex(char* bin, int length) { + std::string s(length * 2, ' '); + for(int i = 0; i < length; i++) { + s[i*2] = digits[(bin[i] >> 4) & 0xf]; + s[i*2+1] = digits[bin[i] & 0xf]; + } + return s; + } + std::string hex(int bin) { + std::string s(sizeof(int) * 2, ' '); + for(int i = 0; i < sizeof(int) * 2; i++) { + s[sizeof(int) * 2 - 1 - i] = digits[bin & 0xf]; + bin = bin >> 4; + } + return s; + } +} + +Hash::Hash() { + finished = false; + + crc32_ctx = 0; + crc32_str = ""; + + ed2k_str = ""; + + MD5_Init(&md5_ctx); + md5_str = ""; + + SHA1_Init(&sha1_ctx); + sha1_str = ""; +} + +void Hash::update(std::string data) { + if(finished) { + throw std::runtime_error("Can't update after digest."); + } + crc32_ctx = CRC32::crc32(crc32_ctx, data.c_str(), data.length()); + ed2k_ctx.update(data.c_str(), data.length()); + MD5_Update(&md5_ctx, data.c_str(), data.length()); + SHA1_Update(&sha1_ctx, data.c_str(), data.length()); +} + +std::string Hash::crc32() { + return Hex::hex(crc32_ctx); +} + +std::string Hash::ed2k() { + if(!ed2k_str.length()) { + finished = true; + char* digest = ed2k_ctx.digest(); + ed2k_str = Hex::hex(digest, 16); + delete digest; + } + return ed2k_str; +} + +std::string Hash::md5() { + if(!md5_str.length()) { + finished = true; + char* digest = new char[16]; + MD5_Final((unsigned char*)digest, &md5_ctx); + md5_str = Hex::hex(digest, 16); + delete digest; + } + return md5_str; +} + +std::string Hash::sha1() { + if(!sha1_str.length()) { + finished = true; + char* digest = new char[20]; + SHA1_Final((unsigned char*)digest, &sha1_ctx); + sha1_str = Hex::hex(digest, 20); + delete digest; + } + return sha1_str; +} diff --git a/src/hash.h b/src/hash.h new file mode 100644 index 0000000..3c7b9c9 --- /dev/null +++ b/src/hash.h @@ -0,0 +1,37 @@ +#ifndef _HASH_H_ +#define _HASH_H_ + +#include + +#include "ed2k.h" + +#include +#include +#include + +class Hash { + private: + bool finished; + + int crc32_ctx; + std::string crc32_str; + + Ed2k ed2k_ctx; + std::string ed2k_str; + + MD5_CTX md5_ctx; + std::string md5_str; + + SHA_CTX sha1_ctx; + std::string sha1_str; + + public: + Hash(); + void update(std::string data); + std::string crc32(); + std::string ed2k(); + std::string md5(); + std::string sha1(); +}; + +#endif // _HASH_H_ diff --git a/src/hash_wrapper.cpp b/src/hash_wrapper.cpp new file mode 100644 index 0000000..a4173b9 --- /dev/null +++ b/src/hash_wrapper.cpp @@ -0,0 +1,14 @@ +#include "hash.h" + +#include +using namespace boost::python; + +BOOST_PYTHON_MODULE(_hash) +{ + class_("Hash") + .def("update", &Hash::update) + .def("crc32", &Hash::crc32) + .def("ed2k", &Hash::ed2k) + .def("md5", &Hash::md5) + .def("sha1", &Hash::sha1); +} -- cgit v1.2.3