// 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) } }
func (w *imageProgressWriter) Write(data []byte) (int, error) { w.mutex.Lock() defer w.mutex.Unlock() if w.internalWriter == nil { var pipeIn *io.PipeReader pipeIn, w.internalWriter = io.Pipe() decoder := json.NewDecoder(pipeIn) go func() { err := w.readProgress(decoder) if err != nil { pipeIn.CloseWithError(err) } }() } return w.internalWriter.Write(data) }
func HandleWrite(filename string, r *io.PipeReader) { r.CloseWithError(fmt.Errorf("Server is Read Only")) }