Skip to content

Commit

Permalink
chore(deepsource): fixed warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
cnlangzi committed Apr 11, 2024
1 parent 1512b4b commit 6cb3c62
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 12 deletions.
2 changes: 1 addition & 1 deletion auth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func createAuthTest(file string) *Auth {

dbTest := sqle.Open(db)

//dbTest.SetMaxOpenConns(1)
// dbTest.SetMaxOpenConns(1)

authTest := NewAuth(dbTest,
WithPrefix("test_"),
Expand Down
22 changes: 11 additions & 11 deletions crypto.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,53 +54,53 @@ func getAESKey(key string) []byte {

func encryptText(plainText, key []byte) (string, error) {

//Create a new Cipher Block from the key
// Create a new Cipher Block from the key
block, err := aes.NewCipher(key)
if err != nil {
return "", err
}

//Create a new GCM - https://en.wikipedia.org/wiki/Galois/Counter_Mode
//https://golang.org/pkg/crypto/cipher/#NewGCM
// Create a new GCM - https://en.wikipedia.org/wiki/Galois/Counter_Mode
// https://golang.org/pkg/crypto/cipher/#NewGCM
aesGCM, err := cipher.NewGCM(block)
if err != nil {
return "", err
}

//Create a nonce. Nonce should be from GCM
// Create a nonce. Nonce should be from GCM
nonce := make([]byte, aesGCM.NonceSize())
if _, err = io.ReadFull(rand.Reader, nonce); err != nil {
return "", err
}

//Encrypt the data using aesGCM.Seal
//Since we don't want to save the nonce somewhere else in this case, we add it as a prefix to the encrypted data. The first nonce argument in Seal is the prefix.
// Encrypt the data using aesGCM.Seal
// Since we don't want to save the nonce somewhere else in this case, we add it as a prefix to the encrypted data. The first nonce argument in Seal is the prefix.
return hex.EncodeToString(aesGCM.Seal(nonce, nonce, plainText, nil)), nil
}

func decryptText(cipherText string, key []byte) (string, error) {

enc, _ := hex.DecodeString(cipherText)

//Create a new Cipher Block from the key
// Create a new Cipher Block from the key
block, err := aes.NewCipher(key)
if err != nil {
return "", err
}

//Create a new GCM
// Create a new GCM
aesGCM, err := cipher.NewGCM(block)
if err != nil {
return "", err
}

//Get the nonce size
// Get the nonce size
nonceSize := aesGCM.NonceSize()

//Extract the nonce from the encrypted data
// Extract the nonce from the encrypted data
nonce, cipherBuf := enc[:nonceSize], enc[nonceSize:]

//Decrypt the data
// Decrypt the data
plainBuf, err := aesGCM.Open(nil, nonce, cipherBuf, nil)
if err != nil {
return "", err
Expand Down

0 comments on commit 6cb3c62

Please sign in to comment.