-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #10 from lmnzx/encryption
Encryption
- Loading branch information
Showing
8 changed files
with
190 additions
and
73 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
package main | ||
|
||
import ( | ||
"crypto/aes" | ||
"crypto/cipher" | ||
"crypto/rand" | ||
"io" | ||
) | ||
|
||
func newEncryptionKey() []byte { | ||
keyBuf := make([]byte, 32) | ||
io.ReadFull(rand.Reader, keyBuf) | ||
return keyBuf | ||
} | ||
|
||
func copyStream(stream cipher.Stream, blockSize int, src io.Reader, dst io.Writer) (int, error) { | ||
var ( | ||
buf = make([]byte, 32*1024) | ||
nw = blockSize | ||
) | ||
for { | ||
n, err := src.Read(buf) | ||
if n > 0 { | ||
stream.XORKeyStream(buf, buf[:n]) | ||
nn, err := dst.Write(buf[:n]) | ||
if err != nil { | ||
return 0, err | ||
} | ||
nw += nn | ||
} | ||
if err == io.EOF { | ||
break | ||
} | ||
if err != nil { | ||
return 0, err | ||
} | ||
} | ||
return nw, nil | ||
} | ||
|
||
func copyDecrypt(key []byte, src io.Reader, dst io.Writer) (int, error) { | ||
block, err := aes.NewCipher(key) | ||
if err != nil { | ||
return 0, err | ||
} | ||
|
||
// read the iv | ||
iv := make([]byte, block.BlockSize()) | ||
if _, err := src.Read(iv); err != nil { | ||
return 0, err | ||
} | ||
|
||
stream := cipher.NewCTR(block, iv) | ||
return copyStream(stream, block.BlockSize(), src, dst) | ||
} | ||
|
||
func copyEncrypt(key []byte, src io.Reader, dst io.Writer) (int, error) { | ||
block, err := aes.NewCipher(key) | ||
if err != nil { | ||
return 0, err | ||
} | ||
|
||
iv := make([]byte, block.BlockSize()) | ||
if _, err := io.ReadFull(rand.Reader, iv); err != nil { | ||
return 0, err | ||
} | ||
|
||
// pprepent iv to the file | ||
if _, err := dst.Write(iv); err != nil { | ||
return 0, nil | ||
} | ||
|
||
stream := cipher.NewCTR(block, iv) | ||
return copyStream(stream, block.BlockSize(), src, dst) | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package main | ||
|
||
import ( | ||
"bytes" | ||
"testing" | ||
) | ||
|
||
func TestCopyEncryptDecrypt(t *testing.T) { | ||
payload := "lemon not lime" | ||
src := bytes.NewReader([]byte(payload)) | ||
dst := new(bytes.Buffer) | ||
key := newEncryptionKey() | ||
_, err := copyEncrypt(key, src, dst) | ||
if err != nil { | ||
t.Error(err) | ||
} | ||
|
||
out := new(bytes.Buffer) | ||
nw, err := copyDecrypt(key, dst, out) | ||
if err != nil { | ||
t.Error(err) | ||
} | ||
|
||
if nw != 16+len(payload) { | ||
t.Fail() | ||
} | ||
|
||
if out.String() != payload { | ||
t.Error("encryption/decryption failed") | ||
} | ||
} |
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Oops, something went wrong.