2021-07-22 23:42:43 +01:00
|
|
|
/*
|
|
|
|
|
Copyright © 2021 Sniptt <support@sniptt.com>
|
|
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
2025-02-07 22:36:53 +00:00
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
2021-07-22 23:42:43 +01:00
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
|
limitations under the License.
|
|
|
|
|
*/
|
2021-07-31 19:28:38 +01:00
|
|
|
package encrypt_test
|
2021-07-22 23:42:43 +01:00
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"crypto/aes"
|
|
|
|
|
"crypto/cipher"
|
|
|
|
|
"testing"
|
2021-07-31 19:28:38 +01:00
|
|
|
|
|
|
|
|
"github.com/sniptt-official/ots/crypto/encrypt"
|
2021-07-22 23:42:43 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func TestBytes(t *testing.T) {
|
2025-02-07 22:36:53 +00:00
|
|
|
t.Run("successfully encrypts and decrypts data", func(t *testing.T) {
|
|
|
|
|
unencryptedSecret := "nuclear_launch_codes"
|
|
|
|
|
|
|
|
|
|
encryptedBytes, err := encrypt.Bytes([]byte(unencryptedSecret))
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatalf("encryption failed: %v", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Use helper methods to get ciphertext without nonce
|
|
|
|
|
ciphertext := encryptedBytes.CiphertextWithoutNonce()
|
|
|
|
|
nonce := encryptedBytes.ExtractNonce()
|
|
|
|
|
|
|
|
|
|
block, err := aes.NewCipher(encryptedBytes.Key)
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatalf("failed to create cipher: %v", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
aesGCM, err := cipher.NewGCM(block)
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatalf("failed to create GCM: %v", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
decryptedSecret, err := aesGCM.Open(nil, nonce, ciphertext, nil)
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatalf("decryption failed: %v", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if got := string(decryptedSecret); got != unencryptedSecret {
|
|
|
|
|
t.Errorf("got %q, want %q", got, unencryptedSecret)
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
t.Run("returns error for empty input", func(t *testing.T) {
|
|
|
|
|
_, err := encrypt.Bytes([]byte{})
|
|
|
|
|
if err == nil {
|
|
|
|
|
t.Error("expected error for empty input, got nil")
|
|
|
|
|
}
|
|
|
|
|
})
|
2021-07-22 23:42:43 +01:00
|
|
|
}
|