本文整理汇总了Golang中golang.org/x/crypto/nacl/box.SealAfterPrecomputation函数的典型用法代码示例。如果您正苦于以下问题:Golang SealAfterPrecomputation函数的具体用法?Golang SealAfterPrecomputation怎么用?Golang SealAfterPrecomputation使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了SealAfterPrecomputation函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: hcWriteClientHello
func (c *Conn) hcWriteClientHello() error {
// Generate a random client nonce.
_, err := io.ReadFull(c.cfg.Rand, c.nonceC.initial[:])
if err != nil {
return err
}
// Ensure client nonce does not have last bit set.
c.nonceC.initial[23] &= 0xFE
// Generate our transient public and private key.
Ct, ct, err := box.GenerateKey(c.cfg.Rand)
if err != nil {
return err
}
c.curveCt = *Ct
c.curvect = *ct
// Send client hello
b := make([]byte, 65, 81)
b[0] = byte(opClientHello)
binary.LittleEndian.PutUint32(b[1:5], clientHelloMagic)
copy(b[9:41], c.curveCt[:])
copy(b[41:65], c.nonceC.initial[:])
var nonce [24]byte
c.nonceC.Next(&nonce)
box.Precompute(&c.curveCtS, &c.curveS, &c.curvect)
b = box.SealAfterPrecomputation(b, nil, &nonce, &c.curveCtS)
return c.conn.WriteFrame(b)
}
示例2: writeBlock
func (w *encryptWriter) writeBlock(b []byte) int {
// Length prefix before the encrypted box so that the
// reader knows how much data should be read;
eBlock := w.encryptedBlockArray[:len(b)+box.Overhead+2]
eBlock[0] = byte(len(b) - 1)
// Length prefix inside the encrypted box so that the
// reader will be able to check that the unencrypted
// length was not tampered with;
cBlock := w.clearBlockArray[:len(b)+1]
cBlock[0] = byte(len(b) - 1)
copy(cBlock[1:], b)
// Encrypt the data.
eBlock = box.SealAfterPrecomputation(eBlock[:1], cBlock, &w.nonce, &w.sharedKey)
w.incrementNonce()
_, w.err = w.Writer.Write(eBlock)
if w.err != nil {
return 0
}
return len(b)
}
示例3: secWriteLoop
// secWriteLoop copies data from pr into w
// doing a nacl seal encryption on the data in the process using shared as the key
func secWriteLoop(w io.Writer, pr *io.PipeReader, shared *[32]byte) {
var failed bool
// check for an error, stops the loop and
// closes the pipe with err to signal the writer we failed
var check = func(err error) {
if err != nil {
log.Println("secWriteLoop err:", err)
if err2 := pr.CloseWithError(err); err2 != nil {
log.Println("CloseWithError failed", err2)
}
failed = true
}
}
for !failed { // until an error occurs
// read the clear message from our pipe
msg := make([]byte, 1024)
n, err := pr.Read(msg)
check(err)
// cut of the unused bytes
msg = msg[:n]
// read 24 bytes of random for our nonce
var nonce [24]byte
_, err = io.ReadFull(rand.Reader, nonce[:])
check(err)
// encrypt and sign our message with the prepended nonce
buf := box.SealAfterPrecomputation(nonce[:], msg, &nonce, shared)
// copy the sealed message with our passed writer
_, err = io.Copy(w, bytes.NewReader(buf))
check(err)
}
}
示例4: SealStrippedEnvelope
func SealStrippedEnvelope(sealed io.Writer, plain io.Reader, ss *[K_SZ]byte,
padding byte, msgNumber uint64) error {
var (
plainBuf bytes.Buffer
cipher []byte
nonce [N_SZ]byte
)
if padding < 1 {
return errors.New("padding must be at lest 1.")
}
_, err := io.Copy(&plainBuf, plain)
if err != nil {
return err
}
paddingBuf := make([]byte, padding)
for i := 0; i < int(padding); i++ {
paddingBuf[i] = padding
}
plainBuf.Write(paddingBuf)
binary.BigEndian.PutUint64(nonce[N_SZ-LN_SZ:], msgNumber)
cipher = box.SealAfterPrecomputation(cipher[:], plainBuf.Bytes(),
&nonce, ss)
sealed.Write(cipher)
return nil
}
示例5: Write
// Write encrypts and writes len(p) bytes from p to the
// underlying data stream.
func (w *SecureWriter) Write(p []byte) (int, error) {
// validate the incoming message
if len(p) == 0 {
return 0, errors.New("nothing to write")
} else if len(p) > maxMessageSize {
return 0, errors.New("message is too large")
}
// calculate the size of the resulting encrypted message
buf := new(bytes.Buffer)
bSize := len(p) + 24 + box.Overhead
err := binary.Write(buf, binary.LittleEndian, uint32(bSize))
if err != nil {
return 0, err
}
size := buf.Bytes()
random := make([]byte, 20)
if _, err := rand.Read(random); err != nil {
return 0, err
}
// first 4 bytes of nonce are the encrypted message size
// last 20 bytes are random
var nonce [24]byte
copy(nonce[0:4], size[:])
copy(nonce[4:24], random[:])
// use shared key to encrypt message
encrypted := box.SealAfterPrecomputation(nonce[:], p, &nonce, w.sharedKey)
w.w.Write(encrypted)
return len(p), nil
}
示例6: Write
func (sw SecureWriter) Write(p []byte) (n int, err error) {
// output buffer will contain [nonce + msgLen + encMsg]
outputBuffer := new(bytes.Buffer)
// Update nonce filling the first 8 bytes with counter
// Counter ensures nonce uniqueness and will take hundreds of years to overflow at uint64
binary.BigEndian.PutUint64(sw.nonce[:], sw.counter)
sw.counter++
// Write nonce
outputBuffer.Write(sw.nonce[:])
ret := box.SealAfterPrecomputation(nil, p, &sw.nonce, &sw.sharedKey)
// Preface encrypted message with length of that message
binary.Write(outputBuffer, binary.BigEndian, uint64(len(ret)))
// copy encrypted message to output buffer
n, err = outputBuffer.Write(ret)
if err != nil {
return n, err
}
// flush output buffer to underlying writer
outputBuffer.WriteTo(sw.writer)
// caller only needs to know how many if it's bytes were written, not the encrypted byte count
return len(p), nil
}
示例7: Write
// Write handles encryption and sends the data off with nonce and length in the
// header. Do note that any error is unrecoverable if data has been sent, as
// the recipient will be unable to understand the stream.
func (srwc SecureReadWriteCloser) Write(p []byte) (int, error) {
// The structure of a packet for reference is:
// [ length uint32 | nonce [24]byte | enc_message []byte ]
// Test if the input is too long
if len(p) > MaxLength {
return 0, ErrOutputTooLong
}
length := uint32(len(p) + NonceBytes + LengthBytes + box.Overhead)
// Prepare the nonce
// Using the same nonce twice reduces the difficulty of "guessing" the key,
// so that's a big no-no, but while random numbers have a risk for collision,
// NaCL docs (http://nacl.cr.yp.to/stream.html) state that, due to the large
// nonce size (24 bytes), that randomly generated nonces have negligible risk
// of collision.
nonce := new([NonceBytes]byte)
if _, err := rand.Read(nonce[:]); err != nil {
return 0, ErrRandom
}
// Make a slice with capacity for the entire thing, and the length set to the
// length/nonce we'll be copying in
message := make([]byte, LengthBytes+NonceBytes, length)
binary.BigEndian.PutUint32(message[0:LengthBytes], length)
copy(message[LengthBytes:LengthBytes+NonceBytes], nonce[:])
// Encrypt!
ret := box.SealAfterPrecomputation(message, p, nonce, srwc.secret)
// Write all the things! We just use this as the return value.
return srwc.writer.Write(ret)
}
示例8: Write
// Write encrypts the given data and writes it to the given
// Writer interface
func (w *SecureWriter) Write(p []byte) (n int, err error) {
generateNonce(w.nonce[:])
encrypted := make([]byte, 1024)
encrypted = box.SealAfterPrecomputation(nil, p, &w.nonce, &w.SharedKey)
n, err = w.Writer.Write(append(w.nonce[:], encrypted...))
return n, err
}
示例9: Write
// Write encrypts the given bytes and writes them to the underlying writer.
//
// Upon any write error, the writer should not be used further as the stream
// may be in an inconsistent state.
func (sw *SecureWriter) Write(p []byte) (int, error) {
// create & write the nonce
var nonce [24]byte
var nonceID = nonce[:8]
var nonceRand = nonce[8:]
binary.BigEndian.PutUint64(nonceID, uint64(time.Now().UnixNano()))
if _, err := rand.Read(nonceRand); err != nil {
return 0, err
}
if _, err := sw.writer.Write(nonce[:]); err != nil {
return 0, err
}
// write the length of the cipher text
var ctLen = make([]byte, 4)
binary.BigEndian.PutUint32(ctLen, uint32(len(p)+box.Overhead))
if _, err := sw.writer.Write(ctLen); err != nil {
return 0, err
}
// generate & write the cipher text
ct := box.SealAfterPrecomputation(nil, p, &nonce, &sw.sharedKey)
if _, err := sw.writer.Write(ct); err != nil {
return 0, err
}
return len(p), nil
}
示例10: Write
// Write encrypted data
func (srw *SecureReadWriter) Write(b []byte) (int, error) {
if srw.writer == nil {
return 0, fmt.Errorf("writer not set")
}
if len(b) > maxLength {
return 0, fmt.Errorf("data is too big")
}
buf := make([]byte, sizeLength+nonceLength, len(b)+box.Overhead+sizeLength+nonceLength)
size := uint64(len(b)) + nonceLength + box.Overhead
binary.PutUvarint(buf[:sizeLength], size)
var nonce [nonceLength]byte
_, err := rand.Read(nonce[:])
if err != nil {
return 0, err
}
copy(buf[sizeLength:], nonce[:])
buf = box.SealAfterPrecomputation(buf, b, &nonce, srw.shared)
_, err = srw.writer.Write(buf)
if err != nil {
return 0, err
}
return len(b), nil
}
示例11: Write
// Write implements io.Writer.
func (w SecureWriter) Write(buf []byte) (int, error) {
if uint64(len(buf)) > maxMessageSize {
return 0, fmt.Errorf("input is too large. Got %d bytes, max: %d", len(buf), maxMessageSize)
}
// Create a nonce.
nonce, err := NewNonce()
if err != nil {
return 0, err
}
debugf("Write: nonce\n%s\n", hex.Dump(nonce[:]))
// Encrypt the message with the nonce prefix.
sealed := box.SealAfterPrecomputation(nonce[:], buf, nonce, w.key)
debugf("Write: sealed %d bytes\n%s\n", len(sealed), hex.Dump(sealed))
// Write a fixed header indicating how long the message is.
header := uint64(len(sealed))
headerSize := binary.Size(header)
if err := binary.Write(w.w, binary.BigEndian, header); err != nil {
return headerSize, err
}
// Write the message.
messageSize, err := w.w.Write(sealed)
// Return the size of the header and the message.
return headerSize + messageSize, err
}
示例12: secureWrite
func secureWrite(w io.Writer, p []byte, sharedKey *[32]byte) (n int, err error) {
if len(p) > MaxMsgLen {
p = p[:MaxMsgLen]
}
//log.Printf("Writing: %s\n", p)
nonce := &[24]byte{}
rand.Read(nonce[:])
//log.Printf("Writing nonce: %v\n", nonce)
n, err = w.Write(nonce[:])
if err != nil {
return
}
o := box.SealAfterPrecomputation(nil, p, nonce, sharedKey)
//log.Printf("Writing encrypted message length: %v\n", uint16(len(o)))
err = binary.Write(w, binary.LittleEndian, uint16(len(o)))
if err != nil {
return
}
//log.Printf("Writing box: %v\n", o)
i, err := w.Write(o)
n = n + i
if err != nil {
return
}
n = len(p)
return
}
示例13: Write
// Write will encrypt message and write it
func (nw NaCLWriter) Write(p []byte) (n int, err error) {
// generate a random nonce
nonc := (&Nonce).GenerateNonce(*nw.Shared)
out := make([]byte, n)
b := box.SealAfterPrecomputation(out, p, &nonc, nw.Shared)
return nw.Writer.Write(b)
}
示例14: Encode
// Encode encrypts a Message and sends it over a Writer
func (enc *Encoder) Encode(msg *Message) error {
// rand.Read is guaranteed to read 24 bytes because it calls ReadFull under the covers
nonceBytes := make([]byte, 24)
_, err := rand.Read(nonceBytes)
if err != nil {
return err
}
// We create a fixed array to copy the nonceBytes (there is no way to convert from slice to fixed array without copying)
var nonce [24]byte
copy(nonce[:], nonceBytes[:])
// box.SealAfterPrecomputation appends the encrypted data to it out and returns it
// We pass nonceBytes to the out parameter so we get returned data in the form [nonce][encryptedData]
data := box.SealAfterPrecomputation(nonceBytes, msg.Data, &nonce, enc.sharedKey)
// Prepend the length to our data so the reader knows how much room to make when reading
var length = uint32(len(data))
err = binary.Write(enc.w, binary.BigEndian, length)
if err != nil {
return nil
}
_, err = enc.w.Write(data)
if err != nil {
return err
}
return nil
}
示例15: Write
// Write encrypts the message and
// writes the result to the underlying data stream.
func (s secureWriter) Write(message []byte) (int, error) {
// Generate a random nonce
nonce, err := generateNonce()
if err != nil {
return 0, errors.New("Failed to encrypt")
}
// Prepend the nonce to out
out := make([]byte, nonceSize)
copy(out[:nonceSize], nonce[:])
// Encrypt the message and prepend to out
out = box.SealAfterPrecomputation(out, message[:], nonce, s.sharedKey)
// Prepend the length of the message in Uint16 to out's beginning
size := make([]byte, 2)
binary.BigEndian.PutUint16(size[:], uint16(len(out)))
out = append(size, out...)
// Write the message decrypted to out
ln, err := s.w.Write(out)
if err != nil {
return 0, err
}
// Return the length of the message wrote to buff
return ln, nil
}