当前位置: 首页>>代码示例>>Golang>>正文


Golang box.OpenAfterPrecomputation函数代码示例

本文整理汇总了Golang中golang.org/x/crypto/nacl/box.OpenAfterPrecomputation函数的典型用法代码示例。如果您正苦于以下问题:Golang OpenAfterPrecomputation函数的具体用法?Golang OpenAfterPrecomputation怎么用?Golang OpenAfterPrecomputation使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。


在下文中一共展示了OpenAfterPrecomputation函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。

示例1: Read

// Read reads the next len(p) bytes from the SecureReader or until the
// SecureReader's buffer is drained.
//
// In the case that the decrypted message does not fit into p, the
// remainder is made available on the next call to Read.
func (r *SecureReader) Read(p []byte) (n int, err error) {
	// Previous short read?
	if r.n > 0 && r.n < len(r.msg) {
		n = copy(p, r.msg[r.n:])
		r.n += n
		return n, err
	}

	nr, err := io.ReadAtLeast(r.r, r.buf, nsize+minBoxSize)
	if nr == 0 {
		return 0, io.EOF
	} else if err == io.ErrUnexpectedEOF {
		return 0, ErrInvalidMessage
	} else if err != nil {
		return 0, err
	}

	// The first nsize bytes of the stream are the nonce; the box then
	// follows.
	copy(r.nonce[:], r.buf[:nsize])

	var ok bool
	r.msg, ok = box.OpenAfterPrecomputation(r.msg[:0], r.buf[nsize:nr], &r.nonce, &r.key)
	n = copy(p, r.msg)
	if !ok {
		return n, ErrOpenBox
	}

	r.n += n
	return n, err
}
开发者ID:billyboar,项目名称:GCSolutions,代码行数:36,代码来源:reader.go

示例2: Read

// Read verifies and decrypts a ciphertext using NACL.
// Wire format:
//  1. nonce (24 bytes)
//  2. size (2 bytes, little endian)
//  3. ciphertext (number of bytes given by previous value)
// Not sure if it would be worth beginning with a magic id.
// This simple implementation is equivalent to a ReadFull - the supplied buffer
// must be big enough to contain the entire plaintext (which is limited to 32K)
// as a successful read will return the entire message.
func (sr secureReader) Read(buf []byte) (int, error) {
	// read the 24 bytes nonce
	var nonce [24]byte
	if _, err := io.ReadFull(sr.r, nonce[:]); err != nil {
		return 0, errors.New("secureReader: cant read nonce: " + err.Error())
	}
	// read the ciphertext size
	var size uint16
	if err := binary.Read(sr.r, binary.LittleEndian, &size); err != nil {
		return 0, errors.New("secureReader: cant read ciphertext size: " + err.Error())
	}
	// check if the supplied buffer is big enough to contain the plain text
	if uint16(len(buf)) < size-box.Overhead {
		return 0, fmt.Errorf("secureReader: ciphertext too long (%d, max = %d)", size, len(buf)+box.Overhead)
	}
	// read the ciphertext
	ctext := make([]byte, size)
	if _, err := io.ReadFull(sr.r, ctext); err != nil {
		return 0, errors.New("secureReader: cant read ciphertext: " + err.Error())
	}
	// verify + decrypt ciphertext into buf and return size of plaintext
	if buf, ok := box.OpenAfterPrecomputation(buf[0:0], ctext, &nonce, &sr.shared); ok {
		return len(buf), nil
	}
	return 0, errors.New("secureReader: error opening box")
}
开发者ID:billyboar,项目名称:GCSolutions,代码行数:35,代码来源:main.go

示例3: readFrame

func (c *Conn) readFrame() ([]byte, opcode, error) {
	b, err := c.conn.ReadFrame()
	if err != nil {
		return nil, 0, err
	}

	if len(b) < 17 {
		return nil, 0, fmt.Errorf("unexpected non-message opcode")
	}

	var nonce [24]byte
	if c.cfg.IsServer {
		c.nonceC.Next(&nonce)
	} else {
		c.nonceS.Next(&nonce)
	}
	b2, ok := box.OpenAfterPrecomputation(nil, b[1:], &nonce, &c.curveCtSt)
	if !ok {
		return nil, 0, fmt.Errorf("invalid msg box received")
	}

	if len(b2) == 0 || b2[0] != 0 {
		return nil, 0, fmt.Errorf("nonzero type byte in msg box")
	}

	return b2[1:], opcode(b[0]), nil
}
开发者ID:hlandau,项目名称:degoutils,代码行数:27,代码来源:curvecp.go

示例4: Read

// Read reads secured data, deciphers it and populates incoming
// buffer p
// The method is thread-safe
func (r *secureReader) Read(p []byte) (n int, err error) {
	r.m.Lock()
	defer r.m.Unlock()

	// read nonce first
	if n, err = r.r.Read(r.nonceBuf[:]); err != nil {
		return
	} else if n != 24 {
		err = errors.New("could not read nonce")
		return
	}

	// read actual message and decipher it
	if n, err = r.r.Read(r.dataBuf[:]); err != nil {
		return
	}

	out, ok := box.OpenAfterPrecomputation([]byte{}, r.dataBuf[:n], &r.nonceBuf, r.key)
	if !ok {
		err = errors.New("could not decrypt incoming message")
		return
	}

	n = copy(p, out)
	if n < len(out) {
		err = fmt.Errorf("buffer is too small. Need to allocate %d bytes, got %d", len(out), n)
	}
	return
}
开发者ID:billyboar,项目名称:GCSolutions,代码行数:32,代码来源:main.go

示例5: secureRead

func secureRead(r io.Reader, p []byte, sharedKey *[32]byte) (n int, err error) {
	if len(p) > MaxMsgLen {
		p = p[:MaxMsgLen]
	}

	nonce := &[24]byte{}
	r.Read(nonce[:])
	//log.Printf("Readed nonce: %v\n", nonce)

	var msgLen uint16
	err = binary.Read(r, binary.LittleEndian, &msgLen)
	if err != nil {
		return
	}
	//log.Printf("Readed message length: %v\n", msgLen)

	b := make([]byte, msgLen)
	n, err = r.Read(b)
	if err != nil {
		return
	}
	b = b[:n]
	//log.Printf("Readed box: %v\n", b)

	o, ok := box.OpenAfterPrecomputation(nil, b, nonce, sharedKey)
	n = copy(p, o)
	//log.Printf("Reading: %s\n", p)
	if ok == false {
		err = ErrDecrypt
	}
	return
}
开发者ID:billyboar,项目名称:GCSolutions,代码行数:32,代码来源:main.go

示例6: Read

// Implementation of io.Reader for SecureReader
// Messages are read and decrypted using the previous initialized keys
func (sr SecureReader) Read(p []byte) (n int, err error) {
	// Get the new nonce
	var nonce [24]byte
	n, err = sr.reader.Read(nonce[:])
	if err != nil {
		return 0, err
	}

	// Followed by length of enc message
	var msgLen uint64
	err = binary.Read(sr.reader, binary.BigEndian, &msgLen)
	if err != nil {
		return 0, err
	}

	// grab encrypted box
	encBox := make([]byte, msgLen)
	n, err = sr.reader.Read(encBox)
	if err != nil {
		return 0, err
	}

	// Read directly into the output slice p[:0]
	ret, valid := box.OpenAfterPrecomputation(p[:0], encBox, &nonce, &sr.sharedKey)
	if !valid {
		return 0, errors.New("Invalid encryption")
	}

	return len(ret), nil
}
开发者ID:billyboar,项目名称:GCSolutions,代码行数:32,代码来源:main.go

示例7: readAndOpenMessage

func (sr *secureReader) readAndOpenMessage() error {
	var totalLength uint16

	// read total length
	err := binary.Read(sr.r, binary.BigEndian, &totalLength)
	if err != nil {
		return err
	}

	// read nonce
	var nonce [nonceSize]byte
	_, err = io.ReadFull(sr.r, nonce[:])
	if err != nil {
		return err
	}

	// read sealed message (length = total length - nonce)
	sealedMessage := make([]byte, totalLength-nonceSize)
	_, err = io.ReadFull(sr.r, sealedMessage)
	if err != nil {
		return err
	}

	// open sealed message
	sr.openedBuffer = sr.openedBuffer[0:0]
	var ok bool
	sr.openedBuffer, ok = box.OpenAfterPrecomputation(sr.openedBuffer, sealedMessage, &nonce, &sr.sharedKey)
	if !ok {
		return errors.New("failed to open NaCl box")
	}
	sr.openedPointer = 0
	return nil
}
开发者ID:billyboar,项目名称:GCSolutions,代码行数:33,代码来源:main.go

示例8: Read

func (s *secureReader) Read(bs []byte) (int, error) {
	if len(s.decrypted) > 0 {
		return s.readDecoded(bs), nil
	}

	// Read the next packet from the underlying reader.
	p := s.packet
	err := p.Read(s.underlying, s.buffer)
	if err != nil {
		return 0, err
	}

	// Decode the packet into bs where possible, bs[:0] will return a slice with 0 length
	// but reuse the underlying array's capacity when appending, avoiding memory allocations.
	decrypted, valid := nacl.OpenAfterPrecomputation(bs[:0], p.data, &p.nonce, &s.sharedKey)
	if !valid {
		return 0, ErrInvalidData
	}

	// If the decrypted data was smaller than the size of bs, then bs contains all the data.
	read := len(p.data) - nacl.Overhead
	if read <= len(bs) {
		s.decrypted = nil
		return read, nil
	}

	// Otherwise, the data was too large for bs, and needs to be copied into bs.
	s.decrypted = decrypted
	return s.readDecoded(bs), nil
}
开发者ID:billyboar,项目名称:GCSolutions,代码行数:30,代码来源:secureio.go

示例9: Read

func (sr SecureReader) Read(p []byte) (int, error) {
	var nonce [24]byte

	_, err := io.ReadFull(sr.r, nonce[:])
	if err != nil {
		return 0, err
	}

	var msgSize uint32
	err = binary.Read(sr.r, binary.LittleEndian, &msgSize)
	if err != nil {
		return 0, err
	}

	// msg := box.SealAfterPrecomputation(nil, p, nonce, sw.sharedKey)
	msg := make([]byte, msgSize)
	_, err = io.ReadFull(sr.r, msg)
	if err != nil {
		return 0, err
	}

	decodedMsg, ok := box.OpenAfterPrecomputation(nil, msg, &nonce, sr.sharedKey)
	if !ok {
		return 0, fmt.Errorf("Failed to decode")
	}
	n := copy(p, decodedMsg)
	return n, nil

}
开发者ID:budevg,项目名称:programming-challenges,代码行数:29,代码来源:main.go

示例10: decryptWithPreShared

func decryptWithPreShared(preSharedKey [keySize]byte, m EncryptedMessage) ([]byte, error) {
	if decryptedMessage, valid := box.OpenAfterPrecomputation(nil, m.data, &m.nonce, &preSharedKey); !valid {
		return nil, MessageDecryptionError
	} else {
		return decryptedMessage, nil
	}
}
开发者ID:sec51,项目名称:cryptoengine,代码行数:7,代码来源:crypto_engine.go

示例11: Read

func (sr SecureReader) Read(decryptedMsg []byte) (n int, err error) {
	nonce := [24]byte{}

	_, err = sr.r.Read(nonce[:])
	if err != nil {
		return 0, err
	}

	msg := [32768]byte{}
	l, err := sr.r.Read(msg[:])
	if err != nil {
		return 0, err
	}
	cryptedMsg := msg[:l]

	// The length of the resulting message is unknown.
	// Passing an empty out will result in everything being passed to overlap
	overlap, b := box.OpenAfterPrecomputation([]byte{}, cryptedMsg, &nonce, sr.shared)
	if !b {
		return 0, errors.New("Decrypt failed")
	}
	copy(decryptedMsg, overlap)

	return len(overlap), nil
}
开发者ID:billyboar,项目名称:GCSolutions,代码行数:25,代码来源:main.go

示例12: Unbox

// Unbox runs the unbox computation given a precomputed key.
func (k PrecomputedSharedKey) Unbox(nonce *saltpack.Nonce, msg []byte) ([]byte, error) {
	ret, ok := box.OpenAfterPrecomputation([]byte{}, msg, (*[24]byte)(nonce), (*[32]byte)(&k))
	if !ok {
		return nil, saltpack.ErrDecryptionFailed
	}
	return ret, nil
}
开发者ID:Varjelus,项目名称:keybase-client,代码行数:8,代码来源:key.go

示例13: Read

// Extended Read method for reader to read secure data
func (sr SecureReader) Read(p []byte) (n int, err error) {
	//get the nonce
	r := make([]byte, nonceSize)
	if _, err = sr.r.Read(r); err != nil {
		return
	}
	var nonce [nonceSize]byte
	copy(nonce[:], r)

	//fetch encrypted message
	enc := make([]byte, maxMessageSize)
	encLen, err := sr.r.Read(enc)
	if err != nil {
		return 0, nil
	}
	enc = enc[:encLen]

	//compute the length of decrypted message
	n = encLen - box.Overhead

	//decrypt
	var s bool
	dec := make([]byte, n)
	if dec, s = box.OpenAfterPrecomputation(nil, enc, &nonce, sr.SecureKeys.share); s != true {
		return 0, errors.New("Error opening the box")
	}

	copy(p, dec)

	return
}
开发者ID:billyboar,项目名称:GCSolutions,代码行数:32,代码来源:secure.go

示例14: Read

// Read encrypted data from the secure reader.
//
// @param p [[]byte] buffer to store decrypted message in
// @return [Integer] number of decrypted bytes read
// @return [Error] if error, or nil
func (secureReader SecureReader) Read(p []byte) (int, error) {
	// Get the encrypted bytes from the internal reader
	data := make([]byte, maxMsgLen)
	n, err := secureReader.reader.Read(data)
	if n == 0 || (err != nil && err != io.EOF) {
		return 0, err
	}

	//  The first set of bytes is the nonce followed by the message.
	var nonce [nonceLength]byte
	for i := 0; i < nonceLength; i++ {
		nonce[i] = data[i]
	}

	decryptedData, success := box.OpenAfterPrecomputation(nil, data[nonceLength:n], &nonce, secureReader.sharedKey)
	if success != true {
		return 0, errors.New("Unable to decrypt message.")
	}

	// Copy results into p
	msgLen := n - nonceLength - box.Overhead
	for i := 0; i < msgLen; i++ {
		p[i] = decryptedData[i]
	}

	return msgLen, nil
}
开发者ID:billyboar,项目名称:GCSolutions,代码行数:32,代码来源:sekurity.go

示例15: Read

// Read satisfies the io.Reader interface. It expects to find the following sequence:
//    1) the fixed length nonce used during encryption of the message
//    2) the length of the original message
//    3) the encrytped message
func (r *SecureReader) Read(p []byte) (n int, err error) {
	// read the nonce
	_, err = io.ReadFull(r.r, r.nonce[:])
	if err != nil {
		return 0, err
	}

	// read the message length
	var mlen uint32
	err = binary.Read(r.r, binary.BigEndian, &mlen)
	if err != nil {
		return 0, err
	}
	if mlen > MaxMessageBytes {
		return 0, ErrMessageTooBig
	}

	// read the encrypted message
	em := r.buf[0 : mlen+box.Overhead]
	n, err = io.ReadFull(r.r, em)
	if err != nil {
		return 0, err
	}

	// decrypt the message
	_, ok := box.OpenAfterPrecomputation(p[0:0], em, &r.nonce, &r.shared)
	if !ok {
		return 0, ErrUndecryptable
	}
	return int(mlen), nil
}
开发者ID:billyboar,项目名称:GCSolutions,代码行数:35,代码来源:main.go


注:本文中的golang.org/x/crypto/nacl/box.OpenAfterPrecomputation函数示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。