GetWriter wraps a crypto StreamWriter using the input key on the input Writer.
(key x.Sensitive, w io.Writer)
| 18 | |
| 19 | // GetWriter wraps a crypto StreamWriter using the input key on the input Writer. |
| 20 | func GetWriter(key x.Sensitive, w io.Writer) (io.Writer, error) { |
| 21 | // No encryption, return the input writer as is. |
| 22 | if key == nil { |
| 23 | return w, nil |
| 24 | } |
| 25 | // Encryption, wrap crypto StreamWriter on the input Writer. |
| 26 | c, err := aes.NewCipher(key) |
| 27 | if err != nil { |
| 28 | return nil, err |
| 29 | } |
| 30 | iv, err := y.GenerateIV() |
| 31 | if err != nil { |
| 32 | return nil, err |
| 33 | } |
| 34 | if iv != nil { |
| 35 | if _, err = w.Write(iv); err != nil { |
| 36 | return nil, err |
| 37 | } |
| 38 | } |
| 39 | return cipher.StreamWriter{S: cipher.NewCTR(c, iv), W: w}, nil |
| 40 | } |
| 41 | |
| 42 | // GetReader wraps a crypto StreamReader using the input key on the input Reader. |
| 43 | func GetReader(key x.Sensitive, r io.Reader) (io.Reader, error) { |
no test coverage detected