Changeset View
Changeset View
Standalone View
Standalone View
src/util/sha1hash.cpp
Context not available. | |||||
31 | { | 31 | { | ||
---|---|---|---|---|---|
32 | SHA1Hash::SHA1Hash() | 32 | SHA1Hash::SHA1Hash() | ||
33 | { | 33 | { | ||
34 | std::fill(hash,hash+20,0); | 34 | memset(hash, 0, 20); | ||
35 | } | 35 | } | ||
36 | 36 | | |||
37 | SHA1Hash::SHA1Hash(const SHA1Hash & other) | 37 | SHA1Hash::SHA1Hash(const SHA1Hash & other) | ||
38 | { | 38 | { | ||
39 | for (int i = 0;i < 20;i++) | 39 | memcpy(hash, other.hash, 20); | ||
40 | hash[i] = other.hash[i]; | | |||
41 | } | 40 | } | ||
42 | 41 | | |||
43 | SHA1Hash::SHA1Hash(const Uint8* h) | 42 | SHA1Hash::SHA1Hash(const Uint8* h) | ||
44 | { | 43 | { | ||
45 | memcpy(hash,h,20); | 44 | memcpy(hash, h, 20); | ||
46 | } | 45 | } | ||
47 | 46 | | |||
48 | 47 | | |||
Context not available. | |||||
51 | 50 | | |||
52 | SHA1Hash & SHA1Hash::operator = (const SHA1Hash & other) | 51 | SHA1Hash & SHA1Hash::operator = (const SHA1Hash & other) | ||
53 | { | 52 | { | ||
54 | for (int i = 0;i < 20;i++) | 53 | memcpy(hash, other.hash, 20); | ||
55 | hash[i] = other.hash[i]; | | |||
56 | return *this; | 54 | return *this; | ||
57 | } | 55 | } | ||
58 | 56 | | |||
59 | bool SHA1Hash::operator == (const SHA1Hash & other) const | 57 | bool SHA1Hash::operator == (const SHA1Hash & other) const | ||
60 | { | 58 | { | ||
61 | for (int i = 0;i < 20;i++) | 59 | return memcmp(hash, other.hash, 20) == 0; | ||
62 | if (hash[i] != other.hash[i]) | | |||
63 | return false; | | |||
64 | | ||||
65 | return true; | | |||
66 | } | 60 | } | ||
67 | 61 | | |||
68 | SHA1Hash SHA1Hash::generate(const Uint8* data,Uint32 len) | 62 | SHA1Hash SHA1Hash::generate(const Uint8* data,Uint32 len) | ||
Context not available. | |||||
72 | return hg.generate(data,len); | 66 | return hg.generate(data,len); | ||
73 | } | 67 | } | ||
74 | 68 | | |||
69 | #define hex_str '%','0','2','x' | ||||
70 | #define hex_str4 hex_str, hex_str, hex_str, hex_str | ||||
71 | #define hex_str20 hex_str4, hex_str4, hex_str4, hex_str4, hex_str4 | ||||
75 | QString SHA1Hash::toString() const | 72 | QString SHA1Hash::toString() const | ||
76 | { | 73 | { | ||
77 | char tmp[41]; | 74 | char tmp[41]; | ||
78 | char fmt[81]; | 75 | char fmt[81] = {hex_str20, '\0'}; | ||
79 | for (int i = 0;i < 20;i++) | 76 | const Uint8* h = getData(); | ||
80 | strncpy(fmt + 4*i,"%02x",4); | 77 | snprintf(tmp, 40, fmt, | ||
81 | fmt[80] = '\0'; | 78 | h[0], h[1], h[2], h[3], h[4], | ||
82 | tmp[40] = '\0'; | 79 | h[5], h[6], h[7], h[8], h[9], | ||
83 | snprintf(tmp,41,fmt, | 80 | h[10], h[11], h[12], h[13], h[14], | ||
84 | hash[0],hash[1],hash[2],hash[3],hash[4], | 81 | h[15], h[16], h[17], h[18], h[19]); | ||
85 | hash[5],hash[6],hash[7],hash[8],hash[9], | 82 | return QString::fromLatin1(tmp, 40); | ||
86 | hash[10],hash[11],hash[12],hash[13],hash[14], | | |||
87 | hash[15],hash[16],hash[17],hash[18],hash[19]); | | |||
88 | return QString::fromUtf8(tmp); | | |||
89 | } | 83 | } | ||
90 | 84 | | |||
91 | QByteArray SHA1Hash::toByteArray() const | 85 | QByteArray SHA1Hash::toByteArray() const | ||
Context not available. | |||||
107 | SHA1Hash operator ^ (const SHA1Hash & a,const SHA1Hash & b) | 101 | SHA1Hash operator ^ (const SHA1Hash & a,const SHA1Hash & b) | ||
108 | { | 102 | { | ||
109 | SHA1Hash k; | 103 | SHA1Hash k; | ||
110 | for (int i = 0;i < 20;i++) | 104 | Uint64* k64 = (Uint64*) k.hash; | ||
111 | { | 105 | const Uint64* a64 = (Uint64*) a.hash; | ||
112 | k.hash[i] = a.hash[i] ^ b.hash[i]; | 106 | const Uint64* b64 = (Uint64*) b.hash; | ||
113 | } | 107 | k64[0] = a64[0] ^ b64[0]; | ||
108 | k64[1] = a64[1] ^ b64[1]; | ||||
109 | k.hash[4] = a.hash[4] ^ b.hash[4]; | ||||
114 | return k; | 110 | return k; | ||
115 | } | 111 | } | ||
116 | 112 | | |||
117 | bool operator < (const SHA1Hash & a,const SHA1Hash & b) | 113 | bool operator < (const SHA1Hash & a,const SHA1Hash & b) | ||
118 | { | 114 | { | ||
119 | for (int i = 0;i < 20;i++) | 115 | return memcmp(a.hash, b.hash, 20) < 0; | ||
120 | { | | |||
121 | if (a.hash[i] < b.hash[i]) | | |||
122 | return true; | | |||
123 | else if (a.hash[i] > b.hash[i]) | | |||
124 | return false; | | |||
125 | } | | |||
126 | | ||||
127 | return false; | | |||
128 | } | 116 | } | ||
129 | 117 | | |||
130 | uint qHash(const SHA1Hash &key) | 118 | uint qHash(const SHA1Hash &key) | ||
Context not available. |