當前位置: 首頁>>代碼示例>>Golang>>正文


Golang cipher.StreamWriter類代碼示例

本文整理匯總了Golang中crypto/cipher.StreamWriter的典型用法代碼示例。如果您正苦於以下問題:Golang StreamWriter類的具體用法?Golang StreamWriter怎麽用?Golang StreamWriter使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。


在下文中一共展示了StreamWriter類的4個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。

示例1: CipherWriter

func CipherWriter(dst io.Writer, cfg *CipherCfg) *cipher.StreamWriter {
	stream := cipher.NewCFBEncrypter(cfg.Algo, cfg.IV)

	enc := new(cipher.StreamWriter)
	enc.S = stream
	enc.W = dst

	return enc
}
開發者ID:NatTuck,項目名稱:defunct-fogsync,代碼行數:9,代碼來源:crypto.go

示例2: newEncWriter

func newEncWriter(w io.Writer, secrets *Secrets) (*encWriter, error) {
	nonce, err := genKey(48)
	if err != nil {
		return nil, err
	}
	digester := hmac.New(sha512.New384, secrets.chunkMaster)
	digester.Write([]byte("\000chunk encryption\000"))
	digester.Write(nonce)
	digester.Write([]byte{0x01, 0x80})
	derivedKey := digester.Sum(nil)
	key := derivedKey[0:32]
	iv := derivedKey[32:48]
	aesCypher, err := aes.NewCipher(key)
	if err != nil {
		return nil, err
	}
	cypher := cipher.NewCTR(aesCypher, iv)
	authHMAC := hmac.New(sha512.New384, secrets.chunkAuthentication)
	writer := io.MultiWriter(w, authHMAC)
	_, err = writer.Write([]byte{0}) // version
	if err != nil {
		return nil, err
	}

	_, err = writer.Write(nonce)
	if err != nil {
		return nil, err
	}

	authHMAC.Write(key)
	authHMAC.Write(iv)
	stream := cipher.StreamWriter{S: cypher, W: writer}
	n, err := stream.Write([]byte{1}) // compression always true
	if err != nil {
		return nil, err
	}
	if n != 1 {
		return nil, fmt.Errorf("Out-of-sync keystream")
	}

	return &encWriter{writer, stream, authHMAC, 50}, nil
}
開發者ID:eadmund,項目名稱:cypherback,代碼行數:42,代碼來源:format.go

示例3: WriteMsg

func (rw *rlpxFrameRW) WriteMsg(msg Msg) error {
	ptype, _ := rlp.EncodeToBytes(msg.Code)

	// write header
	headbuf := make([]byte, 32)
	fsize := uint32(len(ptype)) + msg.Size
	if fsize > maxUint24 {
		return errors.New("message size overflows uint24")
	}
	putInt24(fsize, headbuf) // TODO: check overflow
	copy(headbuf[3:], zeroHeader)
	rw.enc.XORKeyStream(headbuf[:16], headbuf[:16]) // first half is now encrypted

	// write header MAC
	copy(headbuf[16:], updateMAC(rw.egressMAC, rw.macCipher, headbuf[:16]))
	if _, err := rw.conn.Write(headbuf); err != nil {
		return err
	}

	// write encrypted frame, updating the egress MAC hash with
	// the data written to conn.
	tee := cipher.StreamWriter{S: rw.enc, W: io.MultiWriter(rw.conn, rw.egressMAC)}
	if _, err := tee.Write(ptype); err != nil {
		return err
	}
	if _, err := io.Copy(tee, msg.Payload); err != nil {
		return err
	}
	if padding := fsize % 16; padding > 0 {
		if _, err := tee.Write(zero16[:16-padding]); err != nil {
			return err
		}
	}

	// write frame MAC. egress MAC hash is up to date because
	// frame content was written to it as well.
	fmacseed := rw.egressMAC.Sum(nil)
	mac := updateMAC(rw.egressMAC, rw.macCipher, fmacseed)
	_, err := rw.conn.Write(mac)
	return err
}
開發者ID:ruflin,項目名稱:go-ethereum,代碼行數:41,代碼來源:rlpx.go

示例4: writeSecrets

func writeSecrets(secrets *Secrets, backend Backend) (err error) {
	/*
		To write a secrets file:

		PBKDF2 the user's password with a random 32-byte salt
		under SHA-384; use this to generate 80 bytes of keying
		material.  The first 32 bytes form an AES-256
		encryption key; the next 48 bytes form an HMAC-SHA-384
		authentication key.

	*/

	passphrase := termios.PasswordConfirm("Enter passphrase: ", "Repeat passphrase: ")
	salt := make([]byte, 32)
	n, err := rand.Reader.Read(salt)
	if err != nil {
		return err
	}
	if n != 32 {
		return fmt.Errorf("Could not read enough random bytes for salt")
	}
	iterations := 131072 // magic number, about 1 second's worth of time on my lappop
	secretsKeys := pbkdf2.Key([]byte(passphrase), salt, iterations, 80, sha512.New384)
	secretsEncKey := secretsKeys[:32]
	secretsAuthKey := secretsKeys[32:]
	secretsKeysDigest := sha512.New384()
	secretsKeysDigest.Write(secretsKeys)
	secretsKeysHash := secretsKeysDigest.Sum(nil)
	authHMAC := hmac.New(sha512.New384, secretsAuthKey)
	file := bytes.NewBuffer(nil)
	writer := io.MultiWriter(file, authHMAC)
	version := []byte{0}
	n, err = writer.Write(version)
	if err != nil {
		return err
	}
	if n != 1 {
		return fmt.Errorf("Error writing secrets file")
	}

	n, err = writer.Write(salt)
	if err != nil {
		return err
	}
	if n != len(salt) {
		return fmt.Errorf("Error writing secrets file")
	}

	iterBig := big.NewInt(int64(iterations))
	iterBytes := iterBig.Bytes()
	iterBytes = append(make([]byte, 8-len(iterBytes)), iterBytes...)
	n, err = writer.Write(iterBytes)
	if err != nil {
		return err
	}
	if n != len(iterBytes) {
		return fmt.Errorf("Error writing secrets file")
	}

	n, err = writer.Write(secretsKeysHash)
	if err != nil {
		return err
	}
	if n != len(secretsKeysHash) {
		return fmt.Errorf("Error writing secrets file")
	}

	iv, err := genKey(16)
	if err != nil {
		return err
	}
	n, err = writer.Write(iv)
	if err != nil {
		return err
	}
	if n != len(iv) {
		return fmt.Errorf("Error writing secrets file")
	}

	cypher, err := aes.NewCipher(secretsEncKey)
	if err != nil {
		return err
	}

	ctrWriter := cipher.StreamWriter{S: cipher.NewCTR(cypher, iv),
		W: writer}

	n, err = ctrWriter.Write(secrets.metadataMaster)
	if err != nil {
		return err
	}
	if n != len(secrets.metadataMaster) {
		return fmt.Errorf("Error writing secrets file")
	}

	n, err = ctrWriter.Write(secrets.metadataAuthentication)
	if err != nil {
		return err
	}
	if n != len(secrets.metadataAuthentication) {
//.........這裏部分代碼省略.........
開發者ID:eadmund,項目名稱:cypherback,代碼行數:101,代碼來源:secrets.go


注:本文中的crypto/cipher.StreamWriter類示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。