...

Source file src/github.com/ProtonMail/go-crypto/openpgp/canonical_text.go

Documentation: github.com/ProtonMail/go-crypto/openpgp

     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  	"io"
    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 writeCanonical(cw io.Writer, buf []byte, s *int) (int, error) {
    26  	start := 0
    27  	for i, c := range buf {
    28  		switch *s {
    29  		case 0:
    30  			if c == '\r' {
    31  				*s = 1
    32  			} else if c == '\n' {
    33  				cw.Write(buf[start:i])
    34  				cw.Write(newline)
    35  				start = i + 1
    36  			}
    37  		case 1:
    38  			*s = 0
    39  		}
    40  	}
    41  
    42  	cw.Write(buf[start:])
    43  	return len(buf), nil
    44  }
    45  
    46  func (cth *canonicalTextHash) Write(buf []byte) (int, error) {
    47  	return writeCanonical(cth.h, buf, &cth.s)
    48  }
    49  
    50  func (cth *canonicalTextHash) Sum(in []byte) []byte {
    51  	return cth.h.Sum(in)
    52  }
    53  
    54  func (cth *canonicalTextHash) Reset() {
    55  	cth.h.Reset()
    56  	cth.s = 0
    57  }
    58  
    59  func (cth *canonicalTextHash) Size() int {
    60  	return cth.h.Size()
    61  }
    62  
    63  func (cth *canonicalTextHash) BlockSize() int {
    64  	return cth.h.BlockSize()
    65  }
    66  

View as plain text