mirror of
https://github.com/schollz/croc.git
synced 2025-10-11 05:11:06 +02:00
Added Encrypt/Decrypt file
This commit is contained in:
parent
91f31993ec
commit
1727a156db
2 changed files with 75 additions and 0 deletions
47
crypto.go
47
crypto.go
|
@ -4,15 +4,19 @@ import (
|
||||||
"crypto/aes"
|
"crypto/aes"
|
||||||
"crypto/cipher"
|
"crypto/cipher"
|
||||||
"crypto/rand"
|
"crypto/rand"
|
||||||
|
"crypto/sha1"
|
||||||
"crypto/sha256"
|
"crypto/sha256"
|
||||||
"encoding/binary"
|
"encoding/binary"
|
||||||
"encoding/hex"
|
"encoding/hex"
|
||||||
"fmt"
|
"fmt"
|
||||||
mathrand "math/rand"
|
mathrand "math/rand"
|
||||||
|
"os"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/mars9/crypt"
|
||||||
"github.com/schollz/mnemonicode"
|
"github.com/schollz/mnemonicode"
|
||||||
|
log "github.com/sirupsen/logrus"
|
||||||
"golang.org/x/crypto/pbkdf2"
|
"golang.org/x/crypto/pbkdf2"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -76,3 +80,46 @@ func HashBytes(data []byte) string {
|
||||||
sum := sha256.Sum256(data)
|
sum := sha256.Sum256(data)
|
||||||
return fmt.Sprintf("%x", sum)
|
return fmt.Sprintf("%x", sum)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func EncryptFile(inputFilename string, outputFilename string, password string) error {
|
||||||
|
return cryptFile(inputFilename, outputFilename, password, true)
|
||||||
|
}
|
||||||
|
|
||||||
|
func DecryptFile(inputFilename string, outputFilename string, password string) error {
|
||||||
|
return cryptFile(inputFilename, outputFilename, password, false)
|
||||||
|
}
|
||||||
|
|
||||||
|
func cryptFile(inputFilename string, outputFilename string, password string, encrypt bool) error {
|
||||||
|
in, err := os.Open(inputFilename)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
defer in.Close()
|
||||||
|
out, err := os.Create(outputFilename)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
defer func() {
|
||||||
|
if err := out.Sync(); err != nil {
|
||||||
|
log.Error(err)
|
||||||
|
}
|
||||||
|
if err := out.Close(); err != nil {
|
||||||
|
log.Error(err)
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
c := &crypt.Crypter{
|
||||||
|
HashFunc: sha1.New,
|
||||||
|
HashSize: sha1.Size,
|
||||||
|
Key: crypt.NewPbkdf2Key([]byte(password), 32),
|
||||||
|
}
|
||||||
|
if encrypt {
|
||||||
|
if err := c.Encrypt(out, in); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if err := c.Decrypt(out, in); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
|
@ -1,6 +1,8 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"io/ioutil"
|
||||||
|
"os"
|
||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -19,3 +21,29 @@ func TestEncrypt(t *testing.T) {
|
||||||
t.Error("should not work!")
|
t.Error("should not work!")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestEncryptFiles(t *testing.T) {
|
||||||
|
key := GetRandomName()
|
||||||
|
if err := ioutil.WriteFile("temp", []byte("hello, world!"), 0644); err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
}
|
||||||
|
if err := EncryptFile("temp", "temp.enc", key); err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
}
|
||||||
|
if err := DecryptFile("temp.enc", "temp.dec", key); err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
}
|
||||||
|
data, err := ioutil.ReadFile("temp.dec")
|
||||||
|
if string(data) != "hello, world!" {
|
||||||
|
t.Errorf("Got something weird: " + string(data))
|
||||||
|
}
|
||||||
|
if err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
}
|
||||||
|
if err := DecryptFile("temp.enc", "temp.dec", key+"wrong password"); err == nil {
|
||||||
|
t.Error("should throw error!")
|
||||||
|
}
|
||||||
|
os.Remove("temp.dec")
|
||||||
|
os.Remove("temp.enc")
|
||||||
|
|
||||||
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue