当前位置: 首页>>代码示例>>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;未经允许,请勿转载。