Last active
December 9, 2020 02:07
-
-
Save umran/80f89eb7da2cd6f1beeb3a7dc0fe01a8 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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