Encrypt encrypts plaintext with key, which should be a []byte of length KeySize(). It returns an xenc:EncryptedData element.
(key interface{}, plaintext []byte, nonce []byte)
| 32 | // Encrypt encrypts plaintext with key, which should be a []byte of length KeySize(). |
| 33 | // It returns an xenc:EncryptedData element. |
| 34 | func (e CBC) Encrypt(key interface{}, plaintext []byte, nonce []byte) (*etree.Element, error) { |
| 35 | keyBuf, ok := key.([]byte) |
| 36 | if !ok { |
| 37 | return nil, ErrIncorrectKeyType("[]byte") |
| 38 | } |
| 39 | if len(keyBuf) != e.keySize { |
| 40 | return nil, ErrIncorrectKeyLength(e.keySize) |
| 41 | } |
| 42 | |
| 43 | block, err := e.cipher(keyBuf) |
| 44 | if err != nil { |
| 45 | return nil, err |
| 46 | } |
| 47 | |
| 48 | encryptedDataEl := etree.NewElement("xenc:EncryptedData") |
| 49 | encryptedDataEl.CreateAttr("xmlns:xenc", "http://www.w3.org/2001/04/xmlenc#") |
| 50 | { |
| 51 | randBuf := make([]byte, 16) |
| 52 | if _, err := RandReader.Read(randBuf); err != nil { |
| 53 | return nil, err |
| 54 | } |
| 55 | encryptedDataEl.CreateAttr("Id", fmt.Sprintf("_%x", randBuf)) |
| 56 | } |
| 57 | |
| 58 | em := encryptedDataEl.CreateElement("xenc:EncryptionMethod") |
| 59 | em.CreateAttr("Algorithm", e.algorithm) |
| 60 | em.CreateAttr("xmlns:xenc", "http://www.w3.org/2001/04/xmlenc#") |
| 61 | |
| 62 | plaintext = appendPadding(plaintext, block.BlockSize()) |
| 63 | |
| 64 | iv := make([]byte, block.BlockSize()) |
| 65 | if _, err := RandReader.Read(iv); err != nil { |
| 66 | return nil, err |
| 67 | } |
| 68 | |
| 69 | mode := cipher.NewCBCEncrypter(block, iv) |
| 70 | ciphertext := make([]byte, len(plaintext)) |
| 71 | mode.CryptBlocks(ciphertext, plaintext) |
| 72 | ciphertext = append(iv, ciphertext...) |
| 73 | |
| 74 | cd := encryptedDataEl.CreateElement("xenc:CipherData") |
| 75 | cd.CreateAttr("xmlns:xenc", "http://www.w3.org/2001/04/xmlenc#") |
| 76 | cd.CreateElement("xenc:CipherValue").SetText(base64.StdEncoding.EncodeToString(ciphertext)) |
| 77 | return encryptedDataEl, nil |
| 78 | } |
| 79 | |
| 80 | // Decrypt decrypts an encrypted element with key. If the ciphertext contains an |
| 81 | // EncryptedKey element, then the type of `key` is determined by the registered |
nothing calls this directly
no test coverage detected