enc.go 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. package main
  2. import (
  3. "fmt"
  4. "io"
  5. "os"
  6. "github.com/cheggaaa/pb"
  7. "github.com/codegangsta/cli"
  8. "github.com/cryptix/go/crypt"
  9. "github.com/cryptix/go/logging"
  10. )
  11. func runEnc(c *cli.Context) {
  12. inputFname := c.Args().First()
  13. // open the input
  14. input, err := os.Open(inputFname)
  15. logging.CheckFatal(err)
  16. defer input.Close()
  17. // determain the key for the file
  18. key, err := crypt.GetKey(input)
  19. logging.CheckFatal(err)
  20. log.Log("inputkey", fmt.Sprintf("%x", key))
  21. _, err = input.Seek(0, 0)
  22. logging.CheckFatal(err)
  23. // prepare output file
  24. outFname := c.GlobalString("out")
  25. if outFname == "" {
  26. outFname = inputFname + ".enc"
  27. }
  28. output, err := os.Create(outFname)
  29. logging.CheckFatal(err)
  30. defer output.Close()
  31. crypter, err := crypt.NewCrypter(key)
  32. logging.CheckFatal(err)
  33. // create the progress bar
  34. inputStat, err := input.Stat()
  35. logging.CheckFatal(err)
  36. cpipe, err := crypter.MakePipe(output)
  37. logging.CheckFatal(err)
  38. pbar := pb.New64(inputStat.Size()).SetUnits(pb.U_BYTES)
  39. pbar.ShowSpeed = true
  40. pbar.Start()
  41. // write to both to track progress
  42. multi := io.MultiWriter(cpipe, pbar)
  43. // copy the input through the encWriter into the archive
  44. _, err = io.Copy(multi, input)
  45. logging.CheckFatal(err)
  46. pbar.FinishPrint("Encryption done")
  47. }