Standard library / crypto/cipher

crypto/cipher.StreamWriter

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. W is the output writer; S transforms input. Err field is unused. Short writes desynchronize the stream: discard it rather than retrying remaining bytes. Expected stdout: '2 2\n'.

Formal Syntax & Structure

go syntaxgo
type StreamWriter struct {  S   Stream  W   io.Writer  Err error // unused}

Working Example

Example Snippetgo
package main
import (  "bytes"  "crypto/aes"  "crypto/cipher"  "fmt")
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}  n, err := w.Write([]byte("Go"))  check(err)  fmt.Println(n, buf.Len())}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