Skip to content
Snippets Groups Projects
otp_test.go 977 B
Newer Older
  • Learn to ignore specific revisions
  • Malte Bauch's avatar
    Malte Bauch committed
    package crypto
    
    import (
    	"crypto/rand"
    	"testing"
    
    	"github.com/stretchr/testify/assert"
    )
    
    // TestCrypto_OTP tests if a byte array is XOR'ed the proper way.
    func TestCrypto_OTP_XOR(t *testing.T) {
    	testByteArray := []byte{0xF}
    	testByteArray2 := []byte{0xF0}
    	resultByteArray := make([]byte, len(testByteArray))
    
    	for i := range testByteArray {
    		resultByteArray[i] = testByteArray[i] ^ testByteArray2[i]
    	}
    
    	assert.Equal(t, resultByteArray, []byte{0xFF})
    }
    
    func TestCrypto_OTP_EncryptAndDecryptPlaintext(t *testing.T) {
    	secret := []byte("this is a secret")
    
    	key := make([]byte, len(secret))
    	_, err := rand.Read(key)
    	assert.NoError(t, err)
    
    	otp := NewOTP("OTP")
    
    	// encrypt the secret with encrypt method
    	_, encryptedSecret, err := otp.Encrypt(secret, key)
    	assert.NoError(t, err)
    
    	// decrypt the encryptedSecret with decrypt method
    	decryptedSecret, err := otp.Decrypt(nil, encryptedSecret, key)
    	assert.NoError(t, err)
    	assert.Equal(t, secret, decryptedSecret)
    }