cipher.go 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. package osscrypto
  2. import (
  3. "io"
  4. )
  5. // Cipher is interface for encryption or decryption of an object
  6. type Cipher interface {
  7. Encrypter
  8. Decrypter
  9. }
  10. // Encrypter is interface with only encrypt method
  11. type Encrypter interface {
  12. Encrypt(io.Reader) io.Reader
  13. }
  14. // Decrypter is interface with only decrypt method
  15. type Decrypter interface {
  16. Decrypt(io.Reader) io.Reader
  17. }
  18. // CryptoEncrypter provides close method for Encrypter
  19. type CryptoEncrypter struct {
  20. Body io.Reader
  21. Encrypter io.Reader
  22. isClosed bool
  23. }
  24. // Close lets the CryptoEncrypter satisfy io.ReadCloser interface
  25. func (rc *CryptoEncrypter) Close() error {
  26. rc.isClosed = true
  27. if closer, ok := rc.Body.(io.ReadCloser); ok {
  28. return closer.Close()
  29. }
  30. return nil
  31. }
  32. // Read lets the CryptoEncrypter satisfy io.ReadCloser interface
  33. func (rc *CryptoEncrypter) Read(b []byte) (int, error) {
  34. if rc.isClosed {
  35. return 0, io.EOF
  36. }
  37. return rc.Encrypter.Read(b)
  38. }
  39. // CryptoDecrypter provides close method for Decrypter
  40. type CryptoDecrypter struct {
  41. Body io.Reader
  42. Decrypter io.Reader
  43. isClosed bool
  44. }
  45. // Close lets the CryptoDecrypter satisfy io.ReadCloser interface
  46. func (rc *CryptoDecrypter) Close() error {
  47. rc.isClosed = true
  48. if closer, ok := rc.Body.(io.ReadCloser); ok {
  49. return closer.Close()
  50. }
  51. return nil
  52. }
  53. // Read lets the CryptoDecrypter satisfy io.ReadCloser interface
  54. func (rc *CryptoDecrypter) Read(b []byte) (int, error) {
  55. if rc.isClosed {
  56. return 0, io.EOF
  57. }
  58. return rc.Decrypter.Read(b)
  59. }