Beispiel #1
0
// 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)
	}
}
Beispiel #2
0
func PipeRead(pipeReader *io.PipeReader) {
	var (
		err error
		n   int
	)
	data := make([]byte, 1024)
	for n, err = pipeReader.Read(data); err == nil; n, err = pipeReader.Read(data) {
		fmt.Printf("%s\n", data[:n])
	}
	fmt.Println("writer 端 closewitherror后:", err)
}
Beispiel #3
0
func writeCmdOutput(res http.ResponseWriter, pipeReader *io.PipeReader) {
	buffer := make([]byte, BUF_LEN)
	for {
		n, err := pipeReader.Read(buffer)
		if err != nil {
			pipeReader.Close()
			break
		}

		data := buffer[0:n]
		res.Write(data)
		if f, ok := res.(http.Flusher); ok {
			f.Flush()
		}
		//reset buffer
		for i := 0; i < n; i++ {
			buffer[i] = 0
		}
	}
}
Beispiel #4
0
func startRead(reader *io.PipeReader) {
	v := make([]byte, 100)
	_, _ = reader.Read(v)
	fmt.Println(string(v))
}