diff --git a/consts.go b/consts.go index 20028ad..53f8cb2 100644 --- a/consts.go +++ b/consts.go @@ -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 diff --git a/general_test.go b/general_test.go index 3700fb1..ddbaabe 100644 --- a/general_test.go +++ b/general_test.go @@ -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) diff --git a/helpers.go b/helpers.go index 0cd5150..0e863fe 100644 --- a/helpers.go +++ b/helpers.go @@ -2,8 +2,9 @@ package oxicrypt import ( "errors" + "log" "math/rand" - "unicode/utf8" + "strings" ) func formError(errorID string, errorText ...string) error { @@ -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 { diff --git a/impl_aes256.go b/impl_aes256.go index a6c28ce..ddf7084 100644 --- a/impl_aes256.go +++ b/impl_aes256.go @@ -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) { @@ -45,7 +45,7 @@ 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 @@ -53,7 +53,7 @@ func (cipher256 cipherAES256) makePasswordKey(password string) (keyDataOut []byt return []byte(passWithSalt) } -func (cipher256 cipherAES256) GetPasswordKey() []byte { +func (cipher256 *cipherAES256) GetPasswordKey() []byte { return cipher256.passwordKey } @@ -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 }