Skip to content

Commit 6ca009f

Browse files
committed
crypto/openpgp: add package
R=bradfitzgo CC=golang-dev https://golang.org/cl/3989052
1 parent 27ccb41 commit 6ca009f

File tree

11 files changed

+1187
-3
lines changed

11 files changed

+1187
-3
lines changed

src/pkg/Makefile

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,11 @@ DIRS=\
4242
crypto/md4\
4343
crypto/md5\
4444
crypto/ocsp\
45+
crypto/openpgp\
46+
crypto/openpgp/armor\
47+
crypto/openpgp/error\
48+
crypto/openpgp/packet\
49+
crypto/openpgp/s2k\
4550
crypto/rand\
4651
crypto/rc4\
4752
crypto/ripemd160\
@@ -158,6 +163,7 @@ endif
158163

159164
NOTEST=\
160165
crypto\
166+
crypto/openpgp/error\
161167
debug/proc\
162168
exp/draw/x11\
163169
go/ast\

src/pkg/crypto/openpgp/Makefile

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
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+
include ../../../Make.inc
6+
7+
TARG=crypto/openpgp
8+
GOFILES=\
9+
canonical_text.go\
10+
keys.go\
11+
read.go\
12+
write.go\
13+
14+
include ../../../Make.pkg
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
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 (
8+
"hash"
9+
"os"
10+
)
11+
12+
// NewCanonicalTextHash reformats text written to it into the canonical
13+
// form and then applies the hash h. See RFC 4880, section 5.2.1.
14+
func NewCanonicalTextHash(h hash.Hash) hash.Hash {
15+
return &canonicalTextHash{h, 0}
16+
}
17+
18+
type canonicalTextHash struct {
19+
h hash.Hash
20+
s int
21+
}
22+
23+
var newline = []byte{'\r', '\n'}
24+
25+
func (cth *canonicalTextHash) Write(buf []byte) (int, os.Error) {
26+
start := 0
27+
28+
for i, c := range buf {
29+
switch cth.s {
30+
case 0:
31+
if c == '\r' {
32+
cth.s = 1
33+
} else if c == '\n' {
34+
cth.h.Write(buf[start:i])
35+
cth.h.Write(newline)
36+
start = i + 1
37+
}
38+
case 1:
39+
cth.s = 0
40+
}
41+
}
42+
43+
cth.h.Write(buf[start:])
44+
return len(buf), nil
45+
}
46+
47+
func (cth *canonicalTextHash) Sum() []byte {
48+
return cth.h.Sum()
49+
}
50+
51+
func (cth *canonicalTextHash) Reset() {
52+
cth.h.Reset()
53+
cth.s = 0
54+
}
55+
56+
func (cth *canonicalTextHash) Size() int {
57+
return cth.h.Size()
58+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
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 (
8+
"bytes"
9+
"os"
10+
"testing"
11+
)
12+
13+
type recordingHash struct {
14+
buf *bytes.Buffer
15+
}
16+
17+
func (r recordingHash) Write(b []byte) (n int, err os.Error) {
18+
return r.buf.Write(b)
19+
}
20+
21+
func (r recordingHash) Sum() []byte {
22+
return r.buf.Bytes()
23+
}
24+
25+
func (r recordingHash) Reset() {
26+
panic("shouldn't be called")
27+
}
28+
29+
func (r recordingHash) Size() int {
30+
panic("shouldn't be called")
31+
}
32+
33+
34+
func testCanonicalText(t *testing.T, input, expected string) {
35+
r := recordingHash{bytes.NewBuffer(nil)}
36+
c := NewCanonicalTextHash(r)
37+
c.Write([]byte(input))
38+
result := c.Sum()
39+
if expected != string(result) {
40+
t.Errorf("input: %x got: %x want: %x", input, result, expected)
41+
}
42+
}
43+
44+
func TestCanonicalText(t *testing.T) {
45+
testCanonicalText(t, "foo\n", "foo\r\n")
46+
testCanonicalText(t, "foo", "foo")
47+
testCanonicalText(t, "foo\r\n", "foo\r\n")
48+
testCanonicalText(t, "foo\r\nbar", "foo\r\nbar")
49+
testCanonicalText(t, "foo\r\nbar\n\n", "foo\r\nbar\r\n\r\n")
50+
}

0 commit comments

Comments
 (0)