]> git.lizzy.rs Git - plan9front.git/blob - sys/src/liboventi/plan9-sha1.c
Import sources from 2011-03-30 iso image - lib
[plan9front.git] / sys / src / liboventi / plan9-sha1.c
1 #include <u.h>
2 #include <libc.h>
3 #include <oventi.h>
4 #include <libsec.h>
5
6 static void encode(uchar*, u32int*, ulong);
7 extern void vtSha1Block(u32int *s, uchar *p, ulong len);
8
9 struct VtSha1
10 {
11         DigestState *s;
12 };
13
14 VtSha1 *
15 vtSha1Alloc(void)
16 {
17         VtSha1 *s;
18
19         s = vtMemAlloc(sizeof(VtSha1));
20         vtSha1Init(s);
21         return s;
22 }
23
24 void
25 vtSha1Free(VtSha1 *s)
26 {
27         if(s == nil)
28                 return;
29         if(s->s != nil)
30                 free(s->s);
31         vtMemFree(s);
32 }
33
34 void
35 vtSha1Init(VtSha1 *s)
36 {
37         s->s = nil;
38 }
39
40 void
41 vtSha1Update(VtSha1 *s, uchar *p, int len)
42 {
43         s->s = sha1(p, len, nil, s->s);
44 }
45
46 void
47 vtSha1Final(VtSha1 *s, uchar *digest)
48 {
49         sha1(nil, 0, digest, s->s);
50         s->s = nil;
51 }
52
53 void
54 vtSha1(uchar sha1[VtScoreSize], uchar *p, int n)
55 {
56         VtSha1 s;
57
58         vtSha1Init(&s);
59         vtSha1Update(&s, p, n);
60         vtSha1Final(&s, sha1);
61 }
62
63 int
64 vtSha1Check(uchar score[VtScoreSize], uchar *p, int n)
65 {
66         VtSha1 s;
67         uchar score2[VtScoreSize];
68
69         vtSha1Init(&s);
70         vtSha1Update(&s, p, n);
71         vtSha1Final(&s, score2);
72
73         if(memcmp(score, score2, VtScoreSize) != 0) {
74                 vtSetError("vtSha1Check failed");
75                 return 0;
76         }
77         return 1;
78 }