change computer
This commit is contained in:
1 parent
76e166730f
commit
77bfcba5b1
2 files changed
+83
No files matched your search
@@ -0,0 +1,46 @@
|
|||||||
|
package payload
|
||||||
|
|
||||||
|
import (
|
||||||
|
"crypto"
|
||||||
|
"crypto/aes"
|
||||||
|
"crypto/cipher"
|
||||||
|
"crypto/sha512"
|
||||||
|
"errors"
|
||||||
|
"io"
|
||||||
|
)
|
||||||
|
|
||||||
|
var _ = sha512.New
|
||||||
|
|
||||||
|
type Block string
|
||||||
|
|
||||||
|
const AES256 Block = "aes256"
|
||||||
|
|
||||||
|
func hash512BytesOfData(h crypto.Hash, data io.Reader) ([]byte, error) {
|
||||||
|
if !h.Available() {
|
||||||
|
return nil, errors.New("unavailable hash function ")
|
||||||
|
}
|
||||||
|
hf := h.New()
|
||||||
|
if _, err := io.CopyN(hf, data, 512); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return hf.Sum(nil), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func createRandomKey(lengthInBits int, rd io.Reader) ([]byte, error) {
|
||||||
|
if ret, err := hash512BytesOfData(crypto.SHA512, rd); err != nil {
|
||||||
|
return nil, err
|
||||||
|
} else if lengthInBits > len(ret)*8 {
|
||||||
|
return nil, errors.New("asked too long key.")
|
||||||
|
} else {
|
||||||
|
return ret[0 : lengthInBits/8], nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (this Block) CreateBlock(rd io.Reader) (cipher.Block, error) {
|
||||||
|
var err error
|
||||||
|
key := make([]byte, 256/8)
|
||||||
|
if _, err = rd.Read(key); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return aes.NewCipher(key)
|
||||||
|
}
|
||||||
@@ -0,0 +1,37 @@
|
|||||||
|
package payload
|
||||||
|
|
||||||
|
import (
|
||||||
|
"crypto"
|
||||||
|
"github.com/didiercrunch/ecrypto/helper"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestCreateBlock(t *testing.T) {
|
||||||
|
random := helper.NewMockRandomReader()
|
||||||
|
if block, err := AES256.CreateBlock(random); err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
} else {
|
||||||
|
_ = block
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func Testhash512BytesOfData(t *testing.T) {
|
||||||
|
if _, err := hash512BytesOfData(crypto.MD5, helper.NewMockRandomReader()); err == nil {
|
||||||
|
t.Error("should never allow MD5 hash")
|
||||||
|
}
|
||||||
|
rd := helper.NewMockRandomReader()
|
||||||
|
if rd, err := hash512BytesOfData(crypto.SHA512, rd); err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
} else if len(rd) != 512/8 {
|
||||||
|
t.Fail()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestCreateRandomKey(t *testing.T) {
|
||||||
|
if key, err := createRandomKey(128, helper.NewMockRandomReader()); err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
} else if len(key) != 128/8 {
|
||||||
|
t.Error("bad key length. Expected 128 found ", len(key)*8)
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
Reference in new issue
Block a user