GO语言"crypto/cipher"包中"StreamReader"类型的用法及代码示例。
StreamReader 将 Stream 包装到 io.Reader 中。它调用 XORKeyStream 来处理通过的每个数据片。
用法:
type StreamReader struct {
S Stream
R io.Reader
}
例子:
package main
import (
"bytes"
"crypto/aes"
"crypto/cipher"
"encoding/hex"
"io"
"os"
)
func main() {
// Load your secret key from a safe place and reuse it across multiple
// NewCipher calls. (Obviously don't use this example key for anything
// real.) If you want to convert a passphrase to a key, use a suitable
// package like bcrypt or scrypt.
key, _ := hex.DecodeString("6368616e676520746869732070617373")
encrypted, _ := hex.DecodeString("cf0495cc6f75dafc23948538e79904a9")
bReader := bytes.NewReader(encrypted)
block, err := aes.NewCipher(key)
if err != nil {
panic(err)
}
// If the key is unique for each ciphertext, then it's ok to use a zero
// IV.
var iv [aes.BlockSize]byte
stream := cipher.NewOFB(block, iv[:])
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)
}
// Note that this example is simplistic in that it omits any
// authentication of the encrypted data. If you were actually to use
// StreamReader in this manner, an attacker could flip arbitrary bits in
// the output.
}
输出:
some secret text
相关用法
- GO StreamWriter用法及代码示例
- GO StructTag.Lookup用法及代码示例
- GO Strings用法及代码示例
- GO StructTag用法及代码示例
- GO StructOf用法及代码示例
- GO Stringer用法及代码示例
- GO StripPrefix用法及代码示例
- GO Stmt用法及代码示例
- GO Stmt.QueryRowContext用法及代码示例
- GO Scanner.Scan用法及代码示例
- GO Split用法及代码示例
- GO Server.Shutdown用法及代码示例
- GO Slice用法及代码示例
- GO SplitAfter用法及代码示例
- GO Sum256用法及代码示例
- GO SectionReader用法及代码示例
- GO Sin用法及代码示例
- GO Sprintf用法及代码示例
- GO SendMail用法及代码示例
- GO Sprint用法及代码示例
注:本文由纯净天空筛选整理自golang.google.cn大神的英文原创作品 StreamReader。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。