Skip to content

Commit f570b54

Browse files
Yury SmolskyFiloSottile
authored andcommitted
crypto/cipher: make stream examples runnable in the playground
Updates #9679 Change-Id: I53412cf0142364de5f76e8affc15d607bfa2ad23 Reviewed-on: https://go-review.googlesource.com/c/145838 Run-TryBot: Yury Smolsky <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Filippo Valsorda <[email protected]>
1 parent 7836457 commit f570b54

File tree

1 file changed

+16
-27
lines changed

1 file changed

+16
-27
lines changed

src/crypto/cipher/example_test.go

Lines changed: 16 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
package cipher_test
66

77
import (
8+
"bytes"
89
"crypto/aes"
910
"crypto/cipher"
1011
"crypto/rand"
@@ -298,11 +299,8 @@ func ExampleStreamReader() {
298299
// package like bcrypt or scrypt.
299300
key, _ := hex.DecodeString("6368616e676520746869732070617373")
300301

301-
inFile, err := os.Open("encrypted-file")
302-
if err != nil {
303-
panic(err)
304-
}
305-
defer inFile.Close()
302+
encrypted, _ := hex.DecodeString("cf0495cc6f75dafc23948538e79904a9")
303+
bReader := bytes.NewReader(encrypted)
306304

307305
block, err := aes.NewCipher(key)
308306
if err != nil {
@@ -314,22 +312,18 @@ func ExampleStreamReader() {
314312
var iv [aes.BlockSize]byte
315313
stream := cipher.NewOFB(block, iv[:])
316314

317-
outFile, err := os.OpenFile("decrypted-file", os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0600)
318-
if err != nil {
319-
panic(err)
320-
}
321-
defer outFile.Close()
322-
323-
reader := &cipher.StreamReader{S: stream, R: inFile}
324-
// Copy the input file to the output file, decrypting as we go.
325-
if _, err := io.Copy(outFile, reader); err != nil {
315+
reader := &cipher.StreamReader{S: stream, R: bReader}
316+
// Copy the input to the output stream, decrypting as we go.
317+
if _, err := io.Copy(os.Stdout, reader); err != nil {
326318
panic(err)
327319
}
328320

329321
// Note that this example is simplistic in that it omits any
330322
// authentication of the encrypted data. If you were actually to use
331323
// StreamReader in this manner, an attacker could flip arbitrary bits in
332324
// the output.
325+
326+
// Output: some secret text
333327
}
334328

335329
func ExampleStreamWriter() {
@@ -339,11 +333,7 @@ func ExampleStreamWriter() {
339333
// package like bcrypt or scrypt.
340334
key, _ := hex.DecodeString("6368616e676520746869732070617373")
341335

342-
inFile, err := os.Open("plaintext-file")
343-
if err != nil {
344-
panic(err)
345-
}
346-
defer inFile.Close()
336+
bReader := bytes.NewReader([]byte("some secret text"))
347337

348338
block, err := aes.NewCipher(key)
349339
if err != nil {
@@ -355,20 +345,19 @@ func ExampleStreamWriter() {
355345
var iv [aes.BlockSize]byte
356346
stream := cipher.NewOFB(block, iv[:])
357347

358-
outFile, err := os.OpenFile("encrypted-file", os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0600)
359-
if err != nil {
360-
panic(err)
361-
}
362-
defer outFile.Close()
348+
var out bytes.Buffer
363349

364-
writer := &cipher.StreamWriter{S: stream, W: outFile}
365-
// Copy the input file to the output file, encrypting as we go.
366-
if _, err := io.Copy(writer, inFile); err != nil {
350+
writer := &cipher.StreamWriter{S: stream, W: &out}
351+
// Copy the input to the output buffer, encrypting as we go.
352+
if _, err := io.Copy(writer, bReader); err != nil {
367353
panic(err)
368354
}
369355

370356
// Note that this example is simplistic in that it omits any
371357
// authentication of the encrypted data. If you were actually to use
372358
// StreamReader in this manner, an attacker could flip arbitrary bits in
373359
// the decrypted result.
360+
361+
fmt.Printf("%x\n", out.Bytes())
362+
// Output: cf0495cc6f75dafc23948538e79904a9
374363
}

0 commit comments

Comments
 (0)