aboutsummaryrefslogtreecommitdiff
path: root/src/crypto/cipher
diff options
context:
space:
mode:
authorYury Smolsky <yury@smolsky.by>2018-10-30 17:22:05 +0200
committerFilippo Valsorda <filippo@golang.org>2018-10-30 16:23:44 +0000
commitf570b54cc02ffeb82e35e72fc818916e8b896c67 (patch)
treef0112a7466a2254183ae88d4006c905a643f698c /src/crypto/cipher
parent7836457ec3cc128efd9dd54197e8e5c25408c8b0 (diff)
downloadgo-f570b54cc02ffeb82e35e72fc818916e8b896c67.tar.xz
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: Filippo Valsorda <filippo@golang.org>
Diffstat (limited to 'src/crypto/cipher')
-rw-r--r--src/crypto/cipher/example_test.go43
1 files 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
}