|
| 1 | +package eddsa |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + |
| 6 | + cryptobin_test "github.com/deatil/go-cryptobin/tool/test" |
| 7 | +) |
| 8 | + |
| 9 | +func Test_CreateKey(t *testing.T) { |
| 10 | + assertNotEmpty := cryptobin_test.AssertNotEmptyT(t) |
| 11 | + assertNoError := cryptobin_test.AssertNoErrorT(t) |
| 12 | + |
| 13 | + obj := New().GenerateKey() |
| 14 | + |
| 15 | + objPriKey := obj.CreatePrivateKey() |
| 16 | + priKey := objPriKey.ToKeyString() |
| 17 | + |
| 18 | + assertNoError(objPriKey.Error(), "CreateKey-priKey") |
| 19 | + assertNotEmpty(priKey, "CreateKey-priKey") |
| 20 | + |
| 21 | + objPriKeyEn := obj.CreatePrivateKeyWithPassword("123", "AES256CBC", "SHA256") |
| 22 | + priKeyEn := objPriKeyEn.ToKeyString() |
| 23 | + |
| 24 | + assertNoError(objPriKeyEn.Error(), "CreateKey-priKeyEn") |
| 25 | + assertNotEmpty(priKeyEn, "CreateKey-priKeyEn") |
| 26 | + |
| 27 | + objPubKey := obj.CreatePublicKey() |
| 28 | + pubKey := objPubKey.ToKeyString() |
| 29 | + |
| 30 | + assertNoError(objPubKey.Error(), "CreateKey-pubKey") |
| 31 | + assertNotEmpty(pubKey, "CreateKey-pubKey") |
| 32 | +} |
| 33 | + |
| 34 | +func Test_CheckKeyPair(t *testing.T) { |
| 35 | + assertTrue := cryptobin_test.AssertTrueT(t) |
| 36 | + assertNoError := cryptobin_test.AssertNoErrorT(t) |
| 37 | + |
| 38 | + check := New(). |
| 39 | + FromPublicKey([]byte(testPubkey)). |
| 40 | + FromPrivateKey([]byte(testPrikey)) |
| 41 | + checkData := check.CheckKeyPair() |
| 42 | + |
| 43 | + assertNoError(check.Error(), "CheckKeyPair") |
| 44 | + assertTrue(checkData, "CheckKeyPair") |
| 45 | + |
| 46 | + // ========== |
| 47 | + |
| 48 | + checkEn := New(). |
| 49 | + FromPublicKey([]byte(testPubkeyEn)). |
| 50 | + FromPrivateKeyWithPassword([]byte(testPrikeyEn), "123") |
| 51 | + checkDataEn := checkEn.CheckKeyPair() |
| 52 | + |
| 53 | + assertNoError(checkEn.Error(), "CheckKeyPair-EnPri") |
| 54 | + assertTrue(checkDataEn, "CheckKeyPair-EnPri") |
| 55 | +} |
| 56 | + |
| 57 | +func Test_MakePublicKey(t *testing.T) { |
| 58 | + assertEqual := cryptobin_test.AssertEqualT(t) |
| 59 | + assertNoError := cryptobin_test.AssertNoErrorT(t) |
| 60 | + |
| 61 | + ed := New().FromPrivateKey([]byte(testPrikey)) |
| 62 | + newPubkey := ed.MakePublicKey(). |
| 63 | + CreatePublicKey(). |
| 64 | + ToKeyString() |
| 65 | + |
| 66 | + assertNoError(ed.Error(), "MakePublicKey") |
| 67 | + assertEqual(newPubkey, testPubkey, "MakePublicKey") |
| 68 | +} |
| 69 | + |
| 70 | +func Test_CheckKeyString(t *testing.T) { |
| 71 | + ed := New().GenerateKey() |
| 72 | + |
| 73 | + priString := ed.GetPrivateKeyString() |
| 74 | + pubString := ed.GetPublicKeyString() |
| 75 | + |
| 76 | + cryptobin_test.NotEmpty(t, priString) |
| 77 | + cryptobin_test.NotEmpty(t, pubString) |
| 78 | + |
| 79 | + pri := New(). |
| 80 | + FromPrivateKeyString(priString). |
| 81 | + GetPrivateKey() |
| 82 | + pub := New(). |
| 83 | + FromPublicKeyString(pubString). |
| 84 | + GetPublicKey() |
| 85 | + |
| 86 | + cryptobin_test.Equal(t, ed.GetPrivateKey(), pri) |
| 87 | + cryptobin_test.Equal(t, ed.GetPublicKey(), pub) |
| 88 | +} |
0 commit comments