Standard library / crypto/cipher

crypto/cipher.StreamReader

Legacy interoperability only; this mode provides no authentication. Prefer AEAD for new designs. Fixed key and IV below are public test fixtures, never deployment values. R is the input reader; S is the stream transforming read bytes. Read may return data and an error together. Expected stdout: 'Go\n'.

Formal Syntax & Structure

go syntaxgo
type StreamReader struct {  S Stream  R io.Reader}

Working Example

Example Snippetgo
package main
import (  "bytes"  "crypto/aes"  "crypto/cipher"  "fmt"  "io")
func main() {  // Historical interoperability demonstration only. Never deploy this cipher/mode.  b := must(aes.NewCipher(make([]byte, 16)))  iv := make([]byte, 16)  var buf bytes.Buffer  w := cipher.StreamWriter{S: cipher.NewCTR(b, iv), W: &buf}  _, err := w.Write([]byte("Go"))  check(err)  r := cipher.StreamReader{S: cipher.NewCTR(b, iv), R: &buf}  fmt.Println(string(must(io.ReadAll(r))))}func must[T any](v T, e error) T {  if e != nil {    panic(e)  }  return v}func check(e error) {  if e != nil {    panic(e)  }}
Go Reference