]>
Commit | Line | Data |
---|---|---|
1 | // Copyright 2011 The Go Authors. All rights reserved. | |
2 | // Use of this source code is governed by a BSD-style | |
3 | // license that can be found in the LICENSE file. | |
4 | ||
5 | package openpgp | |
6 | ||
7 | import "hash" | |
8 | ||
9 | // NewCanonicalTextHash reformats text written to it into the canonical | |
10 | // form and then applies the hash h. See RFC 4880, section 5.2.1. | |
11 | func NewCanonicalTextHash(h hash.Hash) hash.Hash { | |
12 | return &canonicalTextHash{h, 0} | |
13 | } | |
14 | ||
15 | type canonicalTextHash struct { | |
16 | h hash.Hash | |
17 | s int | |
18 | } | |
19 | ||
20 | var newline = []byte{'\r', '\n'} | |
21 | ||
22 | func (cth *canonicalTextHash) Write(buf []byte) (int, error) { | |
23 | start := 0 | |
24 | ||
25 | for i, c := range buf { | |
26 | switch cth.s { | |
27 | case 0: | |
28 | if c == '\r' { | |
29 | cth.s = 1 | |
30 | } else if c == '\n' { | |
31 | cth.h.Write(buf[start:i]) | |
32 | cth.h.Write(newline) | |
33 | start = i + 1 | |
34 | } | |
35 | case 1: | |
36 | cth.s = 0 | |
37 | } | |
38 | } | |
39 | ||
40 | cth.h.Write(buf[start:]) | |
41 | return len(buf), nil | |
42 | } | |
43 | ||
44 | func (cth *canonicalTextHash) Sum(in []byte) []byte { | |
45 | return cth.h.Sum(in) | |
46 | } | |
47 | ||
48 | func (cth *canonicalTextHash) Reset() { | |
49 | cth.h.Reset() | |
50 | cth.s = 0 | |
51 | } | |
52 | ||
53 | func (cth *canonicalTextHash) Size() int { | |
54 | return cth.h.Size() | |
55 | } | |
56 | ||
57 | func (cth *canonicalTextHash) BlockSize() int { | |
58 | return cth.h.BlockSize() | |
59 | } |