From f570b54cc02ffeb82e35e72fc818916e8b896c67 Mon Sep 17 00:00:00 2001 From: Yury Smolsky Date: Tue, 30 Oct 2018 17:22:05 +0200 Subject: [PATCH] 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 TryBot-Result: Gobot Gobot Reviewed-by: Filippo Valsorda --- src/crypto/cipher/example_test.go | 43 ++++++++++++------------------- 1 file changed, 16 insertions(+), 27 deletions(-) diff --git a/src/crypto/cipher/example_test.go b/src/crypto/cipher/example_test.go index 6e050a9c0d..9c32d6a934 100644 --- a/src/crypto/cipher/example_test.go +++ b/src/crypto/cipher/example_test.go @@ -5,6 +5,7 @@ package cipher_test import ( + "bytes" "crypto/aes" "crypto/cipher" "crypto/rand" @@ -298,11 +299,8 @@ func ExampleStreamReader() { // package like bcrypt or scrypt. key, _ := hex.DecodeString("6368616e676520746869732070617373") - inFile, err := os.Open("encrypted-file") - if err != nil { - panic(err) - } - defer inFile.Close() + encrypted, _ := hex.DecodeString("cf0495cc6f75dafc23948538e79904a9") + bReader := bytes.NewReader(encrypted) block, err := aes.NewCipher(key) if err != nil { @@ -314,15 +312,9 @@ func ExampleStreamReader() { var iv [aes.BlockSize]byte stream := cipher.NewOFB(block, iv[:]) - outFile, err := os.OpenFile("decrypted-file", os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0600) - if err != nil { - panic(err) - } - defer outFile.Close() - - reader := &cipher.StreamReader{S: stream, R: inFile} - // Copy the input file to the output file, decrypting as we go. - if _, err := io.Copy(outFile, reader); err != nil { + reader := &cipher.StreamReader{S: stream, R: bReader} + // Copy the input to the output stream, decrypting as we go. + if _, err := io.Copy(os.Stdout, reader); err != nil { panic(err) } @@ -330,6 +322,8 @@ func ExampleStreamReader() { // authentication of the encrypted data. If you were actually to use // StreamReader in this manner, an attacker could flip arbitrary bits in // the output. + + // Output: some secret text } func ExampleStreamWriter() { @@ -339,11 +333,7 @@ func ExampleStreamWriter() { // package like bcrypt or scrypt. key, _ := hex.DecodeString("6368616e676520746869732070617373") - inFile, err := os.Open("plaintext-file") - if err != nil { - panic(err) - } - defer inFile.Close() + bReader := bytes.NewReader([]byte("some secret text")) block, err := aes.NewCipher(key) if err != nil { @@ -355,15 +345,11 @@ func ExampleStreamWriter() { var iv [aes.BlockSize]byte stream := cipher.NewOFB(block, iv[:]) - outFile, err := os.OpenFile("encrypted-file", os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0600) - if err != nil { - panic(err) - } - defer outFile.Close() + var out bytes.Buffer - writer := &cipher.StreamWriter{S: stream, W: outFile} - // Copy the input file to the output file, encrypting as we go. - if _, err := io.Copy(writer, inFile); err != nil { + writer := &cipher.StreamWriter{S: stream, W: &out} + // Copy the input to the output buffer, encrypting as we go. + if _, err := io.Copy(writer, bReader); err != nil { panic(err) } @@ -371,4 +357,7 @@ func ExampleStreamWriter() { // authentication of the encrypted data. If you were actually to use // StreamReader in this manner, an attacker could flip arbitrary bits in // the decrypted result. + + fmt.Printf("%x\n", out.Bytes()) + // Output: cf0495cc6f75dafc23948538e79904a9 } -- 2.50.0