Skip to content

Commit

Permalink
tests optimization with pprof
Browse files Browse the repository at this point in the history
  • Loading branch information
bykovme committed Nov 19, 2022
1 parent d11a2dd commit 3a19afe
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 20 deletions.
4 changes: 3 additions & 1 deletion consts.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,7 @@ package oxicrypt
// BSENCRPT0001EncKeyIsNotSet - BSENCRPT0001: Password is not set, encryption/decryption is not possible
const BSENCRPT0001EncKeyIsNotSet = "BSENCRPT0001: Encryption key is not created, encryption/decryption is not possible"

//BSENCRPT0002WrongKeyLength - BSENCRPT0002: key length is wrong
// BSENCRPT0002WrongKeyLength - BSENCRPT0002: key length is wrong
const BSENCRPT0002WrongKeyLength = "BSENCRPT0002: key length is wrong"

const cSaltLength = 8
12 changes: 12 additions & 0 deletions general_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,18 @@ func TestRandomStringGenerator(t *testing.T) {
}
}

func TestSaltGenerator(t *testing.T) {
rand.Seed(time.Now().UTC().UnixNano())
salt := generateSalt()
if len(salt) != cSaltLength {
t.Errorf("wrong salt length")
}
salt2 := generateSalt()
if salt == salt2 {
t.Errorf("duplication of the salt")
}
}

func TestRandomStringGeneratorWrongInput(t *testing.T) {
rand.Seed(time.Now().UTC().UnixNano())
lenTo := rand.Intn(50)
Expand Down
50 changes: 36 additions & 14 deletions helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ package oxicrypt

import (
"errors"
"log"
"math/rand"
"unicode/utf8"
"strings"
)

func formError(errorID string, errorText ...string) error {
Expand All @@ -18,28 +19,49 @@ func formError(errorID string, errorText ...string) error {
return errors.New(errorID + ": " + finalText)
}

const cAlphaNum = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz!@#$%^&*()_+=-[]{};:|,./<>?~"

const cMaxLen = len(cAlphaNum)

func generateRandomString(lenFrom, lenTo int) string {
lenMax := lenTo - lenFrom

if lenMax <= 0 {
return ""
}

const alphanum = "0123456789" +
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz" +
"§±!@#$%^&*()_+=-[]{};'\\:\"|,./<>?`~" +
"йцукенгшщзхъфывапролджэёячсмитьбю№ЙЦУКЕНГШЩЗХЪЁЭЖДЛОРПАВЫФЯЧСМИТЬБЮ"
anLen := byte(utf8.RuneCountInString(alphanum))

lenMax = rand.Intn(lenMax) + lenFrom
log.Printf("LenMax: %d", lenMax)
i := 0
var builder strings.Builder

var bytes = make([]byte, lenMax)
rand.Read(bytes)
finalString := ""
for _, b := range bytes {
finalString = finalString + string([]rune(alphanum)[b%anLen])
for i < lenMax {
randI := rand.Intn(cMaxLen)
symbol := cAlphaNum[randI]
_, err := builder.WriteString(string(symbol))
if err != nil {
log.Println(err)
return builder.String()
}
i++
}
return builder.String()
}

func generateSalt() string {
var builder strings.Builder
i := 0

for i < cSaltLength {
randI := rand.Intn(cMaxLen)
symbol := cAlphaNum[randI]
_, err := builder.WriteString(string(symbol))
if err != nil {
log.Println(err)
return builder.String()
}
i++
}
return finalString
return builder.String()
}

func generateRandomBytesWithRandomLen(lenFrom, lenTo int) []byte {
Expand Down
10 changes: 5 additions & 5 deletions impl_aes256.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,11 @@ func (cipher256 *cipherAES256) CleanAndInit() {
cipher256.cachedFinalKey = nil
}

func (cipher256 cipherAES256) GetCryptID() string {
func (cipher256 *cipherAES256) GetCryptID() string {
return cCryptIDAES25601
}

func (cipher256 cipherAES256) GetCipherName() string {
func (cipher256 *cipherAES256) GetCipherName() string {
return cAES256TextDescription
}
func (cipher256 *cipherAES256) SetPassword(password string) (err error) {
Expand All @@ -45,15 +45,15 @@ func (cipher256 *cipherAES256) SetPassword(password string) (err error) {
return nil
}

func (cipher256 cipherAES256) makePasswordKey(password string) (keyDataOut []byte) {
func (cipher256 *cipherAES256) makePasswordKey(password string) (keyDataOut []byte) {
passWithSalt := password + cPassSalt
for len(passWithSalt) < cAESKeyLength {
passWithSalt += passWithSalt
}
return []byte(passWithSalt)
}

func (cipher256 cipherAES256) GetPasswordKey() []byte {
func (cipher256 *cipherAES256) GetPasswordKey() []byte {
return cipher256.passwordKey
}

Expand All @@ -65,7 +65,7 @@ func (cipher256 *cipherAES256) SetPasswordKey(keyDataIn []byte) (err error) {
return nil
}

func (cipher256 cipherAES256) IsPasswordSet() bool {
func (cipher256 *cipherAES256) IsPasswordSet() bool {
if cipher256.passwordKey == nil {
return false
}
Expand Down

0 comments on commit 3a19afe

Please sign in to comment.