Commit f570b54c authored by Yury Smolsky's avatar Yury Smolsky Committed by Filippo Valsorda

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 <yury@smolsky.by>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: default avatarFilippo Valsorda <filippo@golang.org>
parent 7836457e
...@@ -5,6 +5,7 @@ ...@@ -5,6 +5,7 @@
package cipher_test package cipher_test
import ( import (
"bytes"
"crypto/aes" "crypto/aes"
"crypto/cipher" "crypto/cipher"
"crypto/rand" "crypto/rand"
...@@ -298,11 +299,8 @@ func ExampleStreamReader() { ...@@ -298,11 +299,8 @@ func ExampleStreamReader() {
// package like bcrypt or scrypt. // package like bcrypt or scrypt.
key, _ := hex.DecodeString("6368616e676520746869732070617373") key, _ := hex.DecodeString("6368616e676520746869732070617373")
inFile, err := os.Open("encrypted-file") encrypted, _ := hex.DecodeString("cf0495cc6f75dafc23948538e79904a9")
if err != nil { bReader := bytes.NewReader(encrypted)
panic(err)
}
defer inFile.Close()
block, err := aes.NewCipher(key) block, err := aes.NewCipher(key)
if err != nil { if err != nil {
...@@ -314,15 +312,9 @@ func ExampleStreamReader() { ...@@ -314,15 +312,9 @@ func ExampleStreamReader() {
var iv [aes.BlockSize]byte var iv [aes.BlockSize]byte
stream := cipher.NewOFB(block, iv[:]) stream := cipher.NewOFB(block, iv[:])
outFile, err := os.OpenFile("decrypted-file", os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0600) reader := &cipher.StreamReader{S: stream, R: bReader}
if err != nil { // Copy the input to the output stream, decrypting as we go.
panic(err) if _, err := io.Copy(os.Stdout, reader); err != nil {
}
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 {
panic(err) panic(err)
} }
...@@ -330,6 +322,8 @@ func ExampleStreamReader() { ...@@ -330,6 +322,8 @@ func ExampleStreamReader() {
// authentication of the encrypted data. If you were actually to use // authentication of the encrypted data. If you were actually to use
// StreamReader in this manner, an attacker could flip arbitrary bits in // StreamReader in this manner, an attacker could flip arbitrary bits in
// the output. // the output.
// Output: some secret text
} }
func ExampleStreamWriter() { func ExampleStreamWriter() {
...@@ -339,11 +333,7 @@ func ExampleStreamWriter() { ...@@ -339,11 +333,7 @@ func ExampleStreamWriter() {
// package like bcrypt or scrypt. // package like bcrypt or scrypt.
key, _ := hex.DecodeString("6368616e676520746869732070617373") key, _ := hex.DecodeString("6368616e676520746869732070617373")
inFile, err := os.Open("plaintext-file") bReader := bytes.NewReader([]byte("some secret text"))
if err != nil {
panic(err)
}
defer inFile.Close()
block, err := aes.NewCipher(key) block, err := aes.NewCipher(key)
if err != nil { if err != nil {
...@@ -355,15 +345,11 @@ func ExampleStreamWriter() { ...@@ -355,15 +345,11 @@ func ExampleStreamWriter() {
var iv [aes.BlockSize]byte var iv [aes.BlockSize]byte
stream := cipher.NewOFB(block, iv[:]) stream := cipher.NewOFB(block, iv[:])
outFile, err := os.OpenFile("encrypted-file", os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0600) var out bytes.Buffer
if err != nil {
panic(err)
}
defer outFile.Close()
writer := &cipher.StreamWriter{S: stream, W: outFile} writer := &cipher.StreamWriter{S: stream, W: &out}
// Copy the input file to the output file, encrypting as we go. // Copy the input to the output buffer, encrypting as we go.
if _, err := io.Copy(writer, inFile); err != nil { if _, err := io.Copy(writer, bReader); err != nil {
panic(err) panic(err)
} }
...@@ -371,4 +357,7 @@ func ExampleStreamWriter() { ...@@ -371,4 +357,7 @@ func ExampleStreamWriter() {
// authentication of the encrypted data. If you were actually to use // authentication of the encrypted data. If you were actually to use
// StreamReader in this manner, an attacker could flip arbitrary bits in // StreamReader in this manner, an attacker could flip arbitrary bits in
// the decrypted result. // the decrypted result.
fmt.Printf("%x\n", out.Bytes())
// Output: cf0495cc6f75dafc23948538e79904a9
} }
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment