Browse Source

Move functions to appropriate location

master
Jaeha Choi 2 months ago
parent
commit
bb9b3f5d52
  1. 18
      cryptography/aes_gcm_chunks.go
  2. 22
      cryptography/rsa.go
  3. 11
      cryptography/rsa_test.go

18
cryptography/aes_gcm_chunks.go

@ -19,8 +19,9 @@ const (
// ChunkSize is a size of each file chunks in bytes.
// Should be less than max value of uint32 (4294967295)
// since the util package use unsigned 4 bytes to represent the data size.
ChunkSize = 16777216 // 2^24 bytes, about 16.7 MB
IvSize = 12
ChunkSize = 16777216 // 2^24 bytes, about 16.7 MB
IvSize = 12
SymKeySize = 32
// MaxFileSize indicates theoretical limit for the file size. Because chunk number are
// indicated with uint16, MaxFileSize depends on ChunkSize. However, actual file limit
@ -432,6 +433,19 @@ func (ag *AesGcmChunk) decryptBytes(encryptedData []byte, iv []byte) (decryptedD
return decryptedData, nil
}
// genSymKey generates random key for symmetric encryption
func genSymKey() (key []byte, err error) {
// Since we're using AES, generate 32 bytes key for AES256
key = make([]byte, SymKeySize)
// Create random key for symmetric encryption
if _, err := rand.Read(key); err != nil {
log.Debug(err)
log.Error("Error while generating symmetric encryption key")
return nil, err
}
return key, nil
}
// Close closes working file
func (ag *AesGcmChunk) Close() (err error) {
return ag.file.Close()

22
cryptography/rsa.go

@ -6,7 +6,6 @@ import (
"crypto/rsa"
"crypto/sha256"
"crypto/x509"
"encoding/base64"
"encoding/pem"
"github.com/jaeha-choi/Proj_Coconut_Utility/log"
"io/ioutil"
@ -16,7 +15,6 @@ import (
const (
rsaKeyBitSize = 4096
SymKeySize = 32
)
// createRSAKey creates RSA keys with bitSize.
@ -175,19 +173,6 @@ func PemToSha256(pubBlock *pem.Block) []byte {
return hash[:]
}
// genSymKey generates random key for symmetric encryption
func genSymKey() (key []byte, err error) {
// Since we're using AES, generate 32 bytes key for AES256
key = make([]byte, SymKeySize)
// Create random key for symmetric encryption
if _, err := rand.Read(key); err != nil {
log.Debug(err)
log.Error("Error while generating symmetric encryption key")
return nil, err
}
return key, nil
}
// EncryptSignMsg encrypts key for symmetric encryption with receiver's pubic key,
// and sign hashed symmetric encryption key with sender's private key.
func EncryptSignMsg(msg []byte, receiverPubKey *rsa.PublicKey, senderPrivKey *rsa.PrivateKey) (
@ -234,10 +219,3 @@ func DecryptVerifyMsg(encryptedMsg []byte, signature []byte, senderPubKey *rsa.P
return symKey, nil
}
// BytesToBase64 encodes raw bytes to base64
func BytesToBase64(data []byte) []byte {
encoded := make([]byte, base64.StdEncoding.EncodedLen(len(data)))
base64.StdEncoding.Encode(encoded, data[:])
return encoded
}

11
cryptography/rsa_test.go

@ -3,6 +3,7 @@ package cryptography
import (
"bytes"
"crypto/sha256"
"encoding/base64"
"fmt"
"github.com/jaeha-choi/Proj_Coconut_Utility/log"
"os"
@ -76,7 +77,7 @@ func TestGenAESKey(t *testing.T) {
}
func TestBytesToBase64(t *testing.T) {
encoded := BytesToBase64([]byte("test string"))
encoded := BytesToBase64(t, []byte("test string"))
if !bytes.Equal(encoded, []byte("dGVzdCBzdHJpbmc=")) {
t.Error("Error in BytesToBase64")
return
@ -117,3 +118,11 @@ func TestKeyEncryptSignAESKey(t *testing.T) {
return
}
}
// BytesToBase64 encodes raw bytes to base64
func BytesToBase64(t *testing.T, data []byte) []byte {
t.Helper()
encoded := make([]byte, base64.StdEncoding.EncodedLen(len(data)))
base64.StdEncoding.Encode(encoded, data[:])
return encoded
}

Loading…
Cancel
Save