func TestBigFile(t *testing.T) { src := testutil.CreateDummyBuf(147611) dst := &bytes.Buffer{} srcEnc := &bytes.Buffer{} wEnc, err := encrypt.NewWriter(srcEnc, TestKey) if err != nil { t.Errorf("Cannot create write-encryption layer: %v", err) return } if err := wEnc.Close(); err != nil { t.Errorf("Cannot close write-encryption layer: %v", err) return } wDec, err := encrypt.NewReader(bytes.NewReader(srcEnc.Bytes()), TestKey) if err != nil { t.Errorf("Cannot create read-encryption layer: %v", err) return } // Act a bit like the fuse layer: lay := NewLayer(wDec) lay.Truncate(0) bufSize := 128 * 1024 if _, err := io.CopyBuffer(lay, bytes.NewReader(src), make([]byte, bufSize)); err != nil { t.Errorf("Could not encrypt data") return } lay.Truncate(int64(len(src))) if _, err := lay.Seek(0, os.SEEK_SET); err != nil { t.Errorf("Seeking to 0 in big file failed: %v", err) return } n, err := io.CopyBuffer(dst, lay, make([]byte, bufSize)) if err != nil { t.Errorf("Could not copy big file data over overlay: %v", err) return } if n != int64(len(src)) { t.Errorf("Did not fully copy big file: got %d, should be %d bytes", n, len(src)) return } if !bytes.Equal(dst.Bytes(), src) { t.Errorf("Source and destination buffers differ.") return } }
// NewIpfsReader wraps the raw-data reader `r` and returns a Reader // that yields the clear data, if `key` is correct. func NewIpfsReader(key []byte, r Reader) (Reader, error) { rEnc, err := encrypt.NewReader(r, key) if err != nil { return nil, err } rZip := compress.NewReader(rEnc) return reader{ Reader: rZip, Seeker: rZip, WriterTo: rZip, Closer: ioutil.NopCloser(rZip), }, nil }
func main() { opts := parseFlags() if len(opts.args) != 1 { dieWithUsage() } if opts.compress && opts.decompress { dieWithUsage() } if !opts.compress && !opts.decompress { dieWithUsage() } srcPath := opts.args[0] algo, err := compress.FromString(opts.algo) if err != nil { die(err) } src := openSrc(srcPath) defer src.Close() dstPath := dstFilename(opts.compress, srcPath, opts.algo) if opts.useDevNull { dstPath = os.DevNull } dst := openDst(dstPath, opts.forceDstOverwrite) defer dst.Close() pass, err := pwd.PromptPassword() if err != nil { die(err) } key := derivateAesKey([]byte(pass), nil, 32) if key == nil { die(err) } fmt.Println("Processing...") nBytes := int64(0) elapsed := withTime(func() { if opts.compress { ew, err := encrypt.NewWriter(dst, key) if err != nil { die(err) } zw, err := compress.NewWriter(ew, algo) if err != nil { die(err) } nBytes, err = io.Copy(zw, src) if err != nil { die(err) } if err := zw.Close(); err != nil { die(err) } if err := ew.Close(); err != nil { die(err) } } if opts.decompress { er, err := encrypt.NewReader(src, key) if err != nil { die(err) } zr := compress.NewReader(er) nBytes, err = io.Copy(dst, zr) if err != nil { die(err) } } }) fmt.Printf( "%s created, %s processed in %.2f seconds.\n", dstPath, humanize.Bytes(uint64(nBytes)), elapsed.Seconds(), ) }
func main() { opts := parseFlags() if len(opts.args) != 1 { dieWithUsage() } if opts.compress && opts.decompress { dieWithUsage() } if !opts.compress && !opts.decompress { dieWithUsage() } srcPath := opts.args[0] algo, err := compress.FromString(opts.algo) if err != nil { die(err) } src := openSrc(srcPath) defer src.Close() dstPath := dstFilename(opts.compress, srcPath, opts.algo) if opts.useDevNull { dstPath = os.DevNull } dst := openDst(dstPath, opts.forceDstOverwrite) defer dst.Close() key := derivateAesKey([]byte("defaultpassword"), nil, 32) if key == nil { die(err) } var chiper uint16 = aeadCipherAES if opts.encalgo == "chacha" { chiper = aeadCipherChaCha } if opts.encalgo == "aes" { chiper = aeadCipherAES } if opts.encalgo == "none" { opts.encrypt = false } if opts.compress { ew := io.WriteCloser(dst) if opts.encrypt { ew, err = encrypt.NewWriterWithTypeAndBlockSize(dst, key, chiper, opts.maxblocksize) if err != nil { die(err) } } zw, err := compress.NewWriter(ew, algo) if err != nil { die(err) } _, err = io.Copy(zw, src) if err != nil { die(err) } if err := zw.Close(); err != nil { die(err) } if err := ew.Close(); err != nil { die(err) } } if opts.decompress { var reader io.ReadSeeker = src if opts.encrypt { er, err := encrypt.NewReader(src, key) if err != nil { die(err) } reader = er } zr := compress.NewReader(reader) _, err = io.Copy(dst, zr) if err != nil { die(err) } } }