to/fromHex for SHA1Hash. Fix up sha1 handling in db.

This commit is contained in:
Marc Burns 2011-11-10 16:46:17 -05:00
parent a859357adb
commit 831752f665
3 changed files with 20 additions and 5 deletions

View File

@ -103,14 +103,10 @@ uint32_t PosDb::getAccountFromHash(const SHA1Hash & hash) {
std::list<std::string> PosDb::getHashesFromAccount(uint32_t account) {
std::list<std::string> result;
char bfr[20];
ath_it_r range = account_to_hash.equal_range(account);
for(ath_it p = range.first; p != range.second; ++p)
{
p->second.get((unsigned char *)bfr);
result.push_back(bfr);
}
result.push_back(p->second.toHex());
return result;
}

View File

@ -1,6 +1,7 @@
#include "sha1.h"
#include "linus_sha1.h"
#include <cstring>
#include <cstdio>
SHA1Hash::SHA1Hash() {}
@ -19,6 +20,22 @@ SHA1Hash::SHA1Hash(const SHA1Hash & other) {
memcpy(data, other.data, sizeof(unsigned char) * 20);
}
std::string SHA1Hash::toHex() {
char bfr[41];
int c = 0;
for(int i=0;i<20;++i)
snprintf(bfr + 2*i, 3, "%02x", data[i]);
return std::string(bfr);
}
void SHA1Hash::fromHex(std::string s) {
if(s.length() != 40)
return;
for(int i=0;i<20;++i)
data[i] = 16 * ((s[2*i] > '9') ? (s[2*i] - 87) : (s[2*i] - 48))
+ ((s[2*i+1] > '9') ? (s[2*i+1] - 87) : (s[2*i+1] - 48));
}
void SHA1Hash::set(const unsigned char * from) {
memcpy(data, from, sizeof(unsigned char) * 20);
}

View File

@ -8,6 +8,8 @@ struct __attribute__ ((packed)) SHA1Hash {
explicit SHA1Hash(const char * str);
explicit SHA1Hash(const std::string str);
SHA1Hash(const SHA1Hash & other);
std::string toHex();
void fromHex(std::string s);
bool operator==(const SHA1Hash & other) const;
void set(const unsigned char * from);
void get(unsigned char * to) const;