-rw-r--r-- | pwmanager/pwmanager/sha1.h | 75 |
1 files changed, 75 insertions, 0 deletions
diff --git a/pwmanager/pwmanager/sha1.h b/pwmanager/pwmanager/sha1.h new file mode 100644 index 0000000..29442a7 --- a/dev/null +++ b/pwmanager/pwmanager/sha1.h | |||
@@ -0,0 +1,75 @@ | |||
1 | /*************************************************************************** | ||
2 | * * | ||
3 | * copyright (C) 2003 by Michael Buesch * | ||
4 | * email: mbuesch@freenet.de * | ||
5 | * * | ||
6 | * This program is free software; you can redistribute it and/or modify * | ||
7 | * it under the terms of the GNU General Public License version 2 * | ||
8 | * as published by the Free Software Foundation. * | ||
9 | * * | ||
10 | ***************************************************************************/ | ||
11 | |||
12 | /*************************************************************************** | ||
13 | * copyright (C) 2004 by Ulf Schenk | ||
14 | * This file is originaly based on version 1.0.1 of pwmanager | ||
15 | * and was modified to run on embedded devices that run microkde | ||
16 | * | ||
17 | * $Id$ | ||
18 | **************************************************************************/ | ||
19 | |||
20 | #ifndef SHA1_H | ||
21 | #define SHA1_H | ||
22 | |||
23 | #include <stdint.h> | ||
24 | #include <string> | ||
25 | using std::string; | ||
26 | |||
27 | typedef uint8_t byte; | ||
28 | |||
29 | #define SHA1_HASH_LEN_BIT160 | ||
30 | #define SHA1_HASH_LEN_BYTE(SHA1_HASH_LEN_BIT / 8) | ||
31 | |||
32 | /** sha1 hash algorithm. | ||
33 | * Derived from libgcrypt-1.1.12 | ||
34 | */ | ||
35 | class Sha1 | ||
36 | { | ||
37 | struct SHA1_CONTEXT | ||
38 | { | ||
39 | uint32_t h0,h1,h2,h3,h4; | ||
40 | uint32_t nblocks; | ||
41 | byte buf[64]; | ||
42 | int count; | ||
43 | }; | ||
44 | |||
45 | public: | ||
46 | Sha1() { sha1_init(); } | ||
47 | static bool selfTest(); | ||
48 | |||
49 | void sha1_write(const byte *inbuf, uint32_t inlen); | ||
50 | string sha1_read(); | ||
51 | |||
52 | protected: | ||
53 | void sha1_init(); | ||
54 | void sha1_final(); | ||
55 | void burn_stack (int bytes); | ||
56 | void transform(const byte *data); | ||
57 | |||
58 | /** Rotate a 32 bit integer by n bytes */ | ||
59 | uint32_t rol(uint32_t x, int n) | ||
60 | { | ||
61 | #if defined(__GNUC__) && defined(__i386__) | ||
62 | __asm__("roll %%cl,%0" | ||
63 | :"=r" (x) | ||
64 | :"0" (x),"c" (n)); | ||
65 | return x; | ||
66 | #else | ||
67 | return ((x) << (n)) | ((x) >> (32-(n))); | ||
68 | #endif | ||
69 | } | ||
70 | |||
71 | protected: | ||
72 | struct SHA1_CONTEXT ctx; | ||
73 | }; | ||
74 | |||
75 | #endif | ||