5
5
package cipher_test
6
6
7
7
import (
8
+ "bytes"
8
9
"crypto/aes"
9
10
"crypto/cipher"
10
11
"crypto/rand"
@@ -298,11 +299,8 @@ func ExampleStreamReader() {
298
299
// package like bcrypt or scrypt.
299
300
key , _ := hex .DecodeString ("6368616e676520746869732070617373" )
300
301
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 )
306
304
307
305
block , err := aes .NewCipher (key )
308
306
if err != nil {
@@ -314,22 +312,18 @@ func ExampleStreamReader() {
314
312
var iv [aes .BlockSize ]byte
315
313
stream := cipher .NewOFB (block , iv [:])
316
314
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 {
326
318
panic (err )
327
319
}
328
320
329
321
// Note that this example is simplistic in that it omits any
330
322
// authentication of the encrypted data. If you were actually to use
331
323
// StreamReader in this manner, an attacker could flip arbitrary bits in
332
324
// the output.
325
+
326
+ // Output: some secret text
333
327
}
334
328
335
329
func ExampleStreamWriter () {
@@ -339,11 +333,7 @@ func ExampleStreamWriter() {
339
333
// package like bcrypt or scrypt.
340
334
key , _ := hex .DecodeString ("6368616e676520746869732070617373" )
341
335
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" ))
347
337
348
338
block , err := aes .NewCipher (key )
349
339
if err != nil {
@@ -355,20 +345,19 @@ func ExampleStreamWriter() {
355
345
var iv [aes .BlockSize ]byte
356
346
stream := cipher .NewOFB (block , iv [:])
357
347
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
363
349
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 {
367
353
panic (err )
368
354
}
369
355
370
356
// Note that this example is simplistic in that it omits any
371
357
// authentication of the encrypted data. If you were actually to use
372
358
// StreamReader in this manner, an attacker could flip arbitrary bits in
373
359
// the decrypted result.
360
+
361
+ fmt .Printf ("%x\n " , out .Bytes ())
362
+ // Output: cf0495cc6f75dafc23948538e79904a9
374
363
}
0 commit comments