Skip to content

Commit

Permalink
changing unused ReadCloser interface returns to Reader returns
Browse files Browse the repository at this point in the history
  • Loading branch information
shibme committed Sep 17, 2024
1 parent 82cf821 commit a99e951
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 27 deletions.
4 changes: 2 additions & 2 deletions crypto/asx/crypto.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ func (publicKey *PublicKey) NewEncryptingWriter(dst io.Writer, compress bool) (i
}
}

// NewDecryptingReader returns a new ReadCloser that reads and decrypts data with the private key from src.
func (privateKey *PrivateKey) NewDecryptingReader(src io.Reader) (io.ReadCloser, error) {
// NewDecryptingReader returns a new Reader that reads and decrypts data with the private key from src.
func (privateKey *PrivateKey) NewDecryptingReader(src io.Reader) (io.Reader, error) {
algoBytes := make([]byte, 1)
if _, err := io.ReadFull(src, algoBytes); err != nil {
return nil, fmt.Errorf("%s: decrypter failed to read algorithm", "xipher")
Expand Down
4 changes: 2 additions & 2 deletions crypto/ecc/crypto.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ func (publicKey *PublicKey) NewEncryptingWriter(dst io.Writer, compress bool) (i
return (*encrypter.cipher).NewEncryptingWriter(dst, compress)
}

// NewDecryptingReader returns a new ReadCloser that reads and decrypts data with the private key from src.
func (privateKey *PrivateKey) NewDecryptingReader(src io.Reader) (io.ReadCloser, error) {
// NewDecryptingReader returns a new Reader that reads and decrypts data with the private key from src.
func (privateKey *PrivateKey) NewDecryptingReader(src io.Reader) (io.Reader, error) {
ephPubKey := make([]byte, KeyLength)
if _, err := io.ReadFull(src, ephPubKey); err != nil {
return nil, fmt.Errorf("%s: decrypter failed to read ephemeral public key", "xipher")
Expand Down
4 changes: 2 additions & 2 deletions crypto/kyb/cypto.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ func (publicKey *PublicKey) NewEncryptingWriter(dst io.Writer, compress bool) (i
return (*encrypter.cipher).NewEncryptingWriter(dst, compress)
}

// NewDecryptingReader returns a new ReadCloser that reads and decrypts data with the private key from src.
func (privateKey *PrivateKey) NewDecryptingReader(src io.Reader) (io.ReadCloser, error) {
// NewDecryptingReader returns a new Reader that reads and decrypts data with the private key from src.
func (privateKey *PrivateKey) NewDecryptingReader(src io.Reader) (io.Reader, error) {
keyEnc := make([]byte, ctLength)
if _, err := io.ReadFull(src, keyEnc); err != nil {
return nil, fmt.Errorf("%s: decrypter failed to read encapsulated key", "xipher")
Expand Down
32 changes: 16 additions & 16 deletions crypto/xcp/crypto.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,27 +87,27 @@ func (w *Writer) Close() error {
}

type Reader struct {
aead cipher.AEAD
src io.Reader
outBuf bytes.Buffer
nonce []byte
aead cipher.AEAD
src io.Reader
buf bytes.Buffer
nonce []byte
}

// NewDecryptingReader returns a new io.ReadCloser that decrypts src with the cipher
func (cipher *SymmetricCipher) NewDecryptingReader(src io.Reader) (io.ReadCloser, error) {
// NewDecryptingReader returns a new io.Reader that decrypts src with the cipher
func (cipher *SymmetricCipher) NewDecryptingReader(src io.Reader) (io.Reader, error) {
nonce := make([]byte, nonceLength)
if _, err := io.ReadFull(src, nonce); err != nil {
return nil, fmt.Errorf("%s: decrypter failed to read nonce", "xipher")
}
return cipher.newReader(nonce, src)
}

func (cipher *SymmetricCipher) newReader(nonce []byte, src io.Reader) (io.ReadCloser, error) {
func (cipher *SymmetricCipher) newReader(nonce []byte, src io.Reader) (io.Reader, error) {
ciphReader := &Reader{
aead: *cipher.aead,
src: src,
outBuf: bytes.Buffer{},
nonce: nonce,
aead: *cipher.aead,
src: src,
buf: bytes.Buffer{},
nonce: nonce,
}
compressFlag := make([]byte, 1)
if _, err := io.ReadFull(src, compressFlag); err != nil {
Expand All @@ -124,8 +124,8 @@ func (cipher *SymmetricCipher) newReader(nonce []byte, src io.Reader) (io.ReadCl
}

func (r *Reader) Read(p []byte) (int, error) {
if r.outBuf.Len() > len(p) {
return r.outBuf.Read(p)
if r.buf.Len() > len(p) {
return r.buf.Read(p)
}
var block [ctBlockSize]byte
n, err := io.ReadFull(r.src, block[:])
Expand All @@ -134,10 +134,10 @@ func (r *Reader) Read(p []byte) (int, error) {
if err != nil {
return 0, fmt.Errorf("%s: decrypter failed to decrypt", "xipher")
}
r.outBuf.Write(pt)
return r.outBuf.Read(p)
r.buf.Write(pt)
return r.buf.Read(p)
} else if err == io.EOF {
return r.outBuf.Read(p)
return r.buf.Read(p)
} else {
return 0, fmt.Errorf("%s: decrypter failed to read", "xipher")
}
Expand Down
8 changes: 3 additions & 5 deletions xipher_crypto.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ func (publicKey *PublicKey) Encrypt(data []byte, compress bool) (ciphertext []by
return buf.Bytes(), nil
}

func (secretKey *SecretKey) NewDecryptingReader(src io.Reader) (io.ReadCloser, error) {
func (secretKey *SecretKey) NewDecryptingReader(src io.Reader) (io.Reader, error) {
ctTypeBytes := make([]byte, 1)
if _, err := io.ReadFull(src, ctTypeBytes); err != nil {
return nil, fmt.Errorf("%s: decrypter failed to read ciphertext type", "xipher")
Expand Down Expand Up @@ -148,10 +148,8 @@ func (secretKey *SecretKey) DecryptStream(dst io.Writer, src io.Reader) (err err
if err != nil {
return err
}
if _, err = io.Copy(dst, decryptedReader); err != nil {
return err
}
return decryptedReader.Close()
_, err = io.Copy(dst, decryptedReader)
return err
}

// Decrypt decrypts the given ciphertext and returns the decrypted data.
Expand Down

0 comments on commit a99e951

Please sign in to comment.