summaryrefslogtreecommitdiffabout
path: root/pwmanager/pwmanager/sha1.h
blob: c649baea508c781c3d97962795d45e87983ae2e8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
/***************************************************************************
 *                                                                         *
 *   copyright (C) 2003 by Michael Buesch                                  *
 *   email: mbuesch@freenet.de                                             *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License version 2        *
 *   as published by the Free Software Foundation.                         *
 *                                                                         *
 ***************************************************************************/

/***************************************************************************
 * copyright (C) 2004 by Ulf Schenk
 * This file is originaly based on version 1.0.1 of pwmanager
 * and was modified to run on embedded devices that run microkde
 *
 * $Id$
 **************************************************************************/  

#ifndef SHA1_H
#define SHA1_H
#ifdef _WIN32_
#define uint32_t unsigned int
#define uint8_t	unsigned char
#define byte	unsigned char
#else
#include <stdint.h>
typedef uint8_t		byte;
#endif
#include <string>
using std::string;


#define SHA1_HASH_LEN_BIT	160
#define SHA1_HASH_LEN_BYTE	(SHA1_HASH_LEN_BIT / 8)

/** sha1 hash algorithm.
  * Derived from libgcrypt-1.1.12
  */
class Sha1
{
	struct SHA1_CONTEXT
	{
		uint32_t  h0,h1,h2,h3,h4;
		uint32_t  nblocks;
		byte buf[64];
		int  count;
	};

public:
	Sha1() { sha1_init(); }
	static bool selfTest();

	void sha1_write(const byte *inbuf, uint32_t inlen);
	string sha1_read();

protected:
	void sha1_init();
	void sha1_final();
	void burn_stack (int bytes);
	void transform(const byte *data);

	/** Rotate a 32 bit integer by n bytes */
	uint32_t rol(uint32_t x, int n)
	{
#if defined(__GNUC__) && defined(__i386__)
		__asm__("roll %%cl,%0"
			:"=r" (x)
			:"0" (x),"c" (n));
		return x;
#else
		return ((x) << (n)) | ((x) >> (32-(n)));
#endif
	}

protected:
	struct SHA1_CONTEXT ctx;
};

#endif