Пример #1
0
// NewFileReader reads an unencrypted, uncompressed file and
// returns a reader that will yield the data we feed to ipfs.
func NewFileReader(key []byte, r io.Reader, algo compress.AlgorithmType) (or io.Reader, err error) {
	pr, pw := io.Pipe()

	// Setup the writer part:
	wEnc, encErr := encrypt.NewWriter(pw, key)
	if encErr != nil {
		fmt.Println("Enc w fail", err)
		return nil, encErr
	}

	wZip, zipErr := compress.NewWriter(wEnc, algo)
	if zipErr != nil {
		fmt.Println("zip w fail", err)
		return nil, zipErr
	}

	// Suck the reader empty and move it to `wZip`.
	// Every write to wZip will be available as read in `pr`.
	go func() {
		defer func() {
			if zipCloseErr := wZip.Close(); zipCloseErr != nil {
				fmt.Println("wenc close")
				err = zipCloseErr
			}

			if encCloseErr := wEnc.Close(); encCloseErr != nil {
				fmt.Println("wenc close")
				err = encCloseErr
			}

			if pwErr := pw.Close(); pwErr != nil {
				fmt.Println("pw close")
				err = pwErr
			}
		}()

		if _, copyErr := io.Copy(wZip, r); copyErr != nil {
			// TODO: yup.
			fmt.Println("copy f****d up")
			err = copyErr
		}
	}()

	return pr, nil
}
Пример #2
0
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(),
	)
}
Пример #3
0
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)
		}
	}
}