Created
May 22, 2016 23:02
-
-
Save odeke-em/a21249c126d4e0a5033ed51011d67419 to your computer and use it in GitHub Desktop.
CLI for testing out our new Pure Go file encrypter/decrypter
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"flag" | |
"fmt" | |
"io" | |
"os" | |
"github.com/odeke-em/drive/src/dcrypto" | |
) | |
func main() { | |
var password string | |
var decrypt bool | |
flag.StringVar(&password, "password", "", "the password to encrypt/decrypt your files") | |
flag.BoolVar(&decrypt, "decrypt", false, "whether to decrypt or encrypt") | |
flag.Parse() | |
dcryptoFn := func(r io.Reader, password []byte) (rr io.Reader, err error) { | |
if decrypt { | |
rr, err = dcrypto.NewDecrypter(r, password) | |
} else { | |
rr, err = dcrypto.NewEncrypter(r, password) | |
} | |
return | |
} | |
mode := "encrypting" | |
if decrypt { | |
mode = "decrypting" | |
} | |
paths := flag.Args() | |
bPassword := []byte(password) | |
for i, arg := range paths { | |
f, err := os.Open(arg) | |
if err != nil { | |
fmt.Fprintf(os.Stderr, "#%d: err=%v", i, err) | |
continue | |
} | |
rr, err := dcryptoFn(f, bPassword) | |
if err != nil { | |
_ = f.Close() | |
fmt.Fprintf(os.Stderr, "#%d: mode=%s err=%v", i, mode, err) | |
continue | |
} | |
_, _ = io.Copy(os.Stdout, rr) | |
_ = f.Close() | |
if rc, ok := rr.(io.ReadCloser); ok { | |
_ = rc.Close() | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment