Standard library / crypto/cipher

crypto/cipher.StreamReader.Read()

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
func (r StreamReader) Read(dst []byte) (n int, err error)

Parameters & Attributes

NameTypeDescription
dst[]byteDestination for transformed bytes.

Return Value

(int, error)

Bytes read and underlying read error.

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}  p := make([]byte, 2)  _, err = io.ReadFull(r, p)  check(err)  fmt.Println(string(p))}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