Пример #1
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(),
	)
}
Пример #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()

	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)
		}
	}
}