gitlab.com/beacon-software/gadget@v0.0.0-20181217202115-54565ea1ed5e/crypto/encryption.go (about)

     1  package crypto
     2  
     3  // CipherType represents how the message body will be encrypted.
     4  type CipherType uint8
     5  
     6  const (
     7  	// None specifies no encryption. Suitable only for Negotiate requests.
     8  	None CipherType = 0
     9  	// AES symmetric encryption
    10  	AES CipherType = 1
    11  	// RSA asymmetric small message encryption
    12  	RSA CipherType = 2
    13  )
    14  
    15  func (ct CipherType) String() string {
    16  	switch ct {
    17  	case None:
    18  		return "None"
    19  	case AES:
    20  		return "AES"
    21  	case RSA:
    22  		return "RSA"
    23  	default:
    24  		return "Unknown"
    25  	}
    26  }
    27  
    28  // Encryption interface provides the necessary methods for an encryption provider.
    29  type Encryption interface {
    30  	GetType() CipherType
    31  	Encrypt(plaintext []byte) (ciphertext []byte, err error)
    32  	Decrypt(ciphertext []byte) (plaintext []byte, err error)
    33  	Sign(plaintext []byte) (signature []byte, err error)
    34  	Verify(plaintext []byte, signature []byte) (err error)
    35  }
    36  
    37  // NewNoEncryption returns an instance of NoEncryption which can be used as a pass through.
    38  func NewNoEncryption() Encryption {
    39  	return &NoEncryption{}
    40  }
    41  
    42  // NoEncryption provides a passthrough for when you need an Encryption object but don't actually want
    43  // encryption.
    44  type NoEncryption struct{}
    45  
    46  // GetType of cipher on this Encryption.
    47  func (ne *NoEncryption) GetType() CipherType {
    48  	return None
    49  }
    50  
    51  // Encrypt returns the plaintext
    52  func (ne *NoEncryption) Encrypt(plaintext []byte) (ciphertext []byte, err error) {
    53  	return plaintext, nil
    54  }
    55  
    56  // Decrypt returns the ciphertext
    57  func (ne *NoEncryption) Decrypt(ciphertext []byte) (plaintext []byte, err error) {
    58  	return ciphertext, nil
    59  }
    60  
    61  // Sign the passed plaintext and return a signature that can be used to verify that the
    62  // data was signed using this instance of encryptions key.
    63  func (ne *NoEncryption) Sign(plaintext []byte) (signature []byte, err error) {
    64  	return []byte{}, nil
    65  }
    66  
    67  // Verify the passed signature against the key on this instance. Returns err on failure.
    68  func (ne *NoEncryption) Verify(plaintext []byte, signature []byte) (err error) {
    69  	return nil
    70  }