github.com/crewjam/saml@v0.4.14/xmlenc/xmlenc.go (about) 1 // Package xmlenc is a partial implementation of the xmlenc standard 2 // as described in https://www.w3.org/TR/2002/REC-xmlenc-core-20021210/Overview.html. 3 // The purpose of this implementation is to support encrypted SAML assertions. 4 package xmlenc 5 6 import ( 7 "crypto/rand" 8 "hash" 9 10 "github.com/beevik/etree" 11 ) 12 13 // RandReader is a thunk that allows test to replace the source of randomness used by 14 // this package. By default it is Reader from crypto/rand. 15 var RandReader = rand.Reader 16 17 // Encrypter is an interface that encrypts things. Given a plaintext it returns an 18 // XML EncryptedData or EncryptedKey element. The required type of `key` varies 19 // depending on the implementation. 20 type Encrypter interface { 21 Encrypt(key interface{}, plaintext []byte, nonce []byte) (*etree.Element, error) 22 } 23 24 // Decrypter is an interface that decrypts things. The Decrypt() method returns the 25 // plaintext version of the EncryptedData or EncryptedKey element passed. 26 // 27 // You probably don't have to use this interface directly, instead you may call 28 // Decrypt() and it will examine the element to determine which Decrypter to use. 29 type Decrypter interface { 30 Algorithm() string 31 Decrypt(key interface{}, ciphertextEl *etree.Element) ([]byte, error) 32 } 33 34 // DigestMethod represents a digest method such as SHA1, etc. 35 type DigestMethod interface { 36 Algorithm() string 37 Hash() hash.Hash 38 } 39 40 var ( 41 decrypters = map[string]Decrypter{} 42 digestMethods = map[string]DigestMethod{} 43 ) 44 45 // RegisterDecrypter registers the specified decrypter to that it can be 46 // used with Decrypt(). 47 func RegisterDecrypter(d Decrypter) { 48 decrypters[d.Algorithm()] = d 49 } 50 51 // RegisterDigestMethod registers the specified digest method to that it can be 52 // used with Decrypt(). 53 func RegisterDigestMethod(dm DigestMethod) { 54 digestMethods[dm.Algorithm()] = dm 55 } 56 57 // BlockCipher implements a cipher with a fixed size key like AES or 3DES. 58 type BlockCipher interface { 59 Encrypter 60 Decrypter 61 KeySize() int 62 }