Skip to content

Instantly share code, notes, and snippets.

@umran
Last active December 9, 2020 02:07
Show Gist options
  • Save umran/80f89eb7da2cd6f1beeb3a7dc0fe01a8 to your computer and use it in GitHub Desktop.
Save umran/80f89eb7da2cd6f1beeb3a7dc0fe01a8 to your computer and use it in GitHub Desktop.
package cipherstream
import (
"errors"
"io"
)
// CipherStream ...
type CipherStream struct {
buffer chan byte
}
// NewCipherStream ...
func NewCipherStream() *CipherStream {
return &CipherStream{
buffer: make(chan byte),
}
}
// Write ...
func (s *CipherStream) Write(data []byte) (int, error) {
for _, b := range data {
s.buffer <- b
}
return len(data), nil
}
// Close ...
func (s *CipherStream) Close() error {
close(s.buffer)
return nil
}
// Read ...
func (s *CipherStream) Read(dst []byte) (n int, err error) {
if len(dst) == 0 {
return 0, errors.New("invalid slice size")
}
for b := range s.buffer {
dst[n] = b
n++
if n == len(dst)-1 {
return
}
}
err = io.EOF
return
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment