Skip to content

Commit

Permalink
feat: Expose canonical reader
Browse files Browse the repository at this point in the history
  • Loading branch information
lubux committed Jul 20, 2023
1 parent 2a6518b commit 6e5629b
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions openpgp/canonical_text.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
package openpgp

import (
"bytes"
"hash"
"io"
)
Expand All @@ -21,6 +22,12 @@ func NewCanonicalTextWriteCloser(w io.WriteCloser) io.WriteCloser {
return &canonicalTextWriteCloser{w, 0}
}

// NewCanonicalTextReader reformats text read from it into the canonical
// form. See RFC 4880, section 5.2.1.
func NewCanonicalTextReader(r io.Reader) io.Reader {
return &canonicalTextReader{r, bytes.NewBuffer(nil), 0}
}

type canonicalTextHash struct {
h hash.Hash
s int
Expand Down Expand Up @@ -82,3 +89,23 @@ func (tw *canonicalTextWriteCloser) Write(buf []byte) (int, error) {
func (tw *canonicalTextWriteCloser) Close() error {
return tw.w.Close()
}

type canonicalTextReader struct {
r io.Reader
buffer *bytes.Buffer
s int
}

func (tr *canonicalTextReader) Read(buf []byte) (int, error) {
if tr.buffer.Len() > 0 {
return tr.buffer.Read(buf)
}
n, err := tr.r.Read(buf)
if err != nil {
return n, err
}
if _, err = writeCanonical(tr.buffer, buf[:n], &tr.s); err != nil {
return 0, err
}
return tr.buffer.Read(buf)
}

0 comments on commit 6e5629b

Please sign in to comment.