File: state.c

package info (click to toggle)
radare2 0.9.6-3.1%2Bdeb8u1
  • links: PTS, VCS
  • area: main
  • in suites: jessie
  • size: 17,496 kB
  • ctags: 45,959
  • sloc: ansic: 240,999; sh: 3,645; makefile: 2,520; python: 1,212; asm: 312; ruby: 214; awk: 209; perl: 188; lisp: 169; java: 23; xml: 17; php: 6
file content (92 lines) | stat: -rw-r--r-- 2,606 bytes parent folder | download | duplicates (2)
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
81
82
83
84
85
86
87
88
89
90
91
92
/* radare - LGPL - Copyright 2009-2013 pancake<nopcode.org> */

// TODO: use ptr tablez here
#include "r_hash.h"
#include "md5.h"
#include "sha1.h"
#include "sha2.h"
R_API void mdfour(ut8 *out, const ut8 *in, int n);

#define CHKFLAG(f,x) if (f==0||f&x)

R_API RHash *r_hash_new(int rst, int flags) {
	RHash *ctx = R_NEW (RHash);
	if (ctx) {
		r_hash_do_begin (ctx, flags);
		ctx->rst = rst;
	}
	return ctx;
}

R_API void r_hash_do_begin(RHash *ctx, int flags) {
	CHKFLAG (flags, R_HASH_MD5) MD5Init (&ctx->md5);
	CHKFLAG (flags, R_HASH_SHA1) SHA1_Init (&ctx->sha1);
	CHKFLAG (flags, R_HASH_SHA256) SHA256_Init (&ctx->sha256);
	CHKFLAG (flags, R_HASH_SHA384) SHA384_Init (&ctx->sha384);
	CHKFLAG (flags, R_HASH_SHA512) SHA512_Init (&ctx->sha512);
	ctx->rst = 0;
}

R_API void r_hash_do_end(RHash *ctx, int flags) {
	CHKFLAG (flags, R_HASH_MD5) MD5Final (ctx->digest, &ctx->md5);
	CHKFLAG (flags, R_HASH_SHA1) SHA1_Final (ctx->digest, &ctx->sha1);
	CHKFLAG (flags, R_HASH_SHA256) SHA256_Final (ctx->digest, &ctx->sha256);
	CHKFLAG (flags, R_HASH_SHA384) SHA384_Final (ctx->digest, &ctx->sha384);
	CHKFLAG (flags, R_HASH_SHA512) SHA512_Final (ctx->digest, &ctx->sha512);
	ctx->rst = 1;
}

R_API void r_hash_free(RHash *ctx) {
	free (ctx);
}

R_API ut8 *r_hash_do_md5(RHash *ctx, const ut8 *input, ut32 len) {
	if (ctx->rst)
		MD5Init (&ctx->md5);
	if (len>0)
		MD5Update (&ctx->md5, input, len);
	if (ctx->rst || len == 0)
		MD5Final (&ctx->digest, &ctx->md5);
	return ctx->digest;
}

R_API ut8 *r_hash_do_sha1(RHash *ctx, const ut8 *input, ut32 len) {
	if (ctx->rst)
		SHA1_Init (&ctx->sha1);
	SHA1_Update (&ctx->sha1, input, len);
	if (ctx->rst || len == 0)
		SHA1_Final (ctx->digest, &ctx->sha1);
	return ctx->digest;
}

R_API ut8 *r_hash_do_md4(RHash *ctx, const ut8 *input, ut32 len) {
	mdfour (ctx->digest, input, len);
	return ctx->digest;
}

R_API ut8 *r_hash_do_sha256(RHash *ctx, const ut8 *input, ut32 len) {
	if (ctx->rst)
		SHA256_Init (&ctx->sha256);
	SHA256_Update (&ctx->sha256, input, len);
	if (ctx->rst || len == 0)
		SHA256_Final (ctx->digest, &ctx->sha256);
	return ctx->digest;
}

R_API ut8 *r_hash_do_sha384(RHash *ctx, const ut8 *input, ut32 len) {
	if (ctx->rst)
		SHA384_Init (&ctx->sha384);
	SHA384_Update (&ctx->sha384, input, len);
	if (ctx->rst || len == 0)
		SHA384_Final (ctx->digest, &ctx->sha384);
	return ctx->digest;
}

R_API ut8 *r_hash_do_sha512(RHash *ctx, const ut8 *input, ut32 len) {
	if (ctx->rst)
		SHA512_Init (&ctx->sha512);
	SHA512_Update (&ctx->sha512, input, len);
	if (ctx->rst || len == 0)
		SHA512_Final (ctx->digest, &ctx->sha512);
	return ctx->digest;
}