amuz.es/src/go/misc@v1.0.1/crypto/secret.go (about)

     1  package crypto
     2  
     3  import (
     4  	"crypto/rand"
     5  	"github.com/minio/sio"
     6  	"io"
     7  )
     8  
     9  /*
    10  NewSecretBox returns a SecretBox object with a provided key.
    11  
    12  Here is the validation code written for Python:
    13  	import os,binascii
    14  	key=binascii.b2a_base64(os.urandom(32),newline=False)
    15  	print(key.decode('us-ascii'))
    16  */
    17  func NewSecretBox(key []byte) SecretBox {
    18  	streamConfig := sio.Config{
    19  		Key:  key,
    20  		Rand: rand.Reader,
    21  	}
    22  	var (
    23  		encryptReader = func(src io.Reader) (io.Reader, error) {
    24  			return sio.EncryptReader(src, streamConfig)
    25  		}
    26  		decryptReader = func(src io.Reader) (io.Reader, error) {
    27  			return sio.DecryptReader(src, streamConfig)
    28  		}
    29  		encryptWriter = func(dst io.Writer) (io.WriteCloser, error) {
    30  			return sio.EncryptWriter(dst, streamConfig)
    31  		}
    32  		decryptWriter = func(dst io.Writer) (io.WriteCloser, error) {
    33  			return sio.DecryptWriter(dst, streamConfig)
    34  		}
    35  	)
    36  	return &secretBoxImpl{
    37  		encryptReaderGen: encryptReader,
    38  		decryptReaderGen: decryptReader,
    39  		encryptWriterGen: encryptWriter,
    40  		decryptWriterGen: decryptWriter,
    41  	}
    42  }
    43  
    44  // SecretBox is an encryption and decryption provider.
    45  type SecretBox interface {
    46  	// NewEncryptReader returns EncryptReader from given io.Reader.
    47  	NewEncryptReader(io.Reader) (io.Reader, error)
    48  	// NewDecryptReader returns DecryptReader from given io.Reader.
    49  	NewDecryptReader(src io.Reader) (io.Reader, error)
    50  	// NewEncryptWriter returns EncryptWriter from given io.Writer.
    51  	NewEncryptWriter(dst io.Writer) (io.WriteCloser, error)
    52  	// NewDecryptWriter returns DecryptWriter from given io.Writer.
    53  	NewDecryptWriter(dst io.Writer) (io.WriteCloser, error)
    54  	// EncryptedSize returns encrypted data size from given raw data size.
    55  	EncryptedSize(size uint64) (uint64, error)
    56  	// DecryptedSize returns decrypted data size from given raw data size.
    57  	DecryptedSize(size uint64) (uint64, error)
    58  }
    59  
    60  type secretBoxImpl struct {
    61  	encryptReaderGen func(src io.Reader) (io.Reader, error)
    62  	decryptReaderGen func(src io.Reader) (io.Reader, error)
    63  	encryptWriterGen func(dst io.Writer) (io.WriteCloser, error)
    64  	decryptWriterGen func(dst io.Writer) (io.WriteCloser, error)
    65  }
    66  
    67  // 인터페이스가 실제 dto랑 호환되는가
    68  var _ SecretBox = (*secretBoxImpl)(nil)
    69  
    70  func (x *secretBoxImpl) NewEncryptReader(r io.Reader) (io.Reader, error) {
    71  	return x.encryptReaderGen(r)
    72  }
    73  func (x *secretBoxImpl) NewDecryptReader(r io.Reader) (io.Reader, error) {
    74  	return x.decryptReaderGen(r)
    75  }
    76  func (x *secretBoxImpl) NewEncryptWriter(w io.Writer) (io.WriteCloser, error) {
    77  	return x.encryptWriterGen(w)
    78  }
    79  func (x *secretBoxImpl) NewDecryptWriter(w io.Writer) (io.WriteCloser, error) {
    80  	return x.decryptWriterGen(w)
    81  }
    82  func (x *secretBoxImpl) EncryptedSize(size uint64) (uint64, error) { return sio.EncryptedSize(size) }
    83  func (x *secretBoxImpl) DecryptedSize(size uint64) (uint64, error) { return sio.DecryptedSize(size) }