|
| 1 | +package ed521 |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "crypto" |
| 6 | + "testing" |
| 7 | + |
| 8 | + cryptobin_test "github.com/deatil/go-cryptobin/tool/test" |
| 9 | +) |
| 10 | + |
| 11 | +var ( |
| 12 | + testPrikey = `-----BEGIN PRIVATE KEY----- |
| 13 | +MFcCAQAwDgYKKwYBBAGC3CwCAQYABEIAKG1VJuFCsAxsDCr6uBrNfQaYCPOvGu7D |
| 14 | +kLJ/CQkUhCmg0DKrQrhLWwwp6TXUekXr5LOlo0rzl7frT+g46YSbiQI= |
| 15 | +-----END PRIVATE KEY----- |
| 16 | + ` |
| 17 | + testPubkey = `-----BEGIN PUBLIC KEY----- |
| 18 | +MFUwDgYKKwYBBAGC3CwCAQYAA0MAmni8ls4g54Kqpk5gMM7/dscHeV6nm4Eq8zY3 |
| 19 | +OX1BYZZaEp6KX4P7PhwHv5ZYhHZK23bH4O483nRnAkitNHNKMLoA |
| 20 | +-----END PUBLIC KEY----- |
| 21 | +` |
| 22 | + |
| 23 | + testPrikeyEn = `-----BEGIN ENCRYPTED PRIVATE KEY----- |
| 24 | +MIHDMF8GCSqGSIb3DQEFDTBSMDEGCSqGSIb3DQEFDDAkBBAUciNePUdR/1Yam7+s |
| 25 | +ESzEAgInEDAMBggqhkiG9w0CCQUAMB0GCWCGSAFlAwQBKgQQPSnhje7xky4oXvb4 |
| 26 | +JRNkqARgHbRbcy46pxZL3nMYvZDHrgAYmdzSMaDNXqtASgKtc2YMH0vi/3Ej9ai9 |
| 27 | +OWfCAC6k7Jf8QmH4SSV73ubvcpi+WXGMqqAnPh52HWN7apIyrUvBRz2n2bz5EEBp |
| 28 | +FbfFSxL1 |
| 29 | +-----END ENCRYPTED PRIVATE KEY----- |
| 30 | + ` |
| 31 | + testPubkeyEn = `-----BEGIN PUBLIC KEY----- |
| 32 | +MFUwDgYKKwYBBAGC3CwCAQYAA0MAlFBP2O8rujvdW3bE3SoLJfGe3P33p0RyHZkW |
| 33 | +Rvx91ZZQXrNe4vEgylU2BcQm3nXMm2Z8rB6fWr9fEKu57WU6ENgB |
| 34 | +-----END PUBLIC KEY----- |
| 35 | + ` |
| 36 | +) |
| 37 | + |
| 38 | +func testED521Sign(t *testing.T, opts *Options) { |
| 39 | + assertTrue := cryptobin_test.AssertTrueT(t) |
| 40 | + assertNoError := cryptobin_test.AssertNoErrorT(t) |
| 41 | + assertNotEmpty := cryptobin_test.AssertNotEmptyT(t) |
| 42 | + |
| 43 | + data := []byte("test-passtest-passtest-passtest-passtest-passtest-passtest-passtest-passtest-pass3333333333333333333333333333333333333333333333333333test-pa2222222222222222222222222222222222222222222sstest-passt111111111111111111111111111111111est-passtest-passtest-passtest-passtest-passtest-passtest-passtest-passtest-passtest-passtest-passtest-passtest-passtest-passtest-passtest-passtest-passtest-passtest-passtest-passtest-passtest-passtest-passtest-passtest-passtest-passtest-passtest-passtest-passtest-passtest-passtest-passtest-passtest-passtest-passtest-passtest-passtest-passtest-passtest-passtest-passtest-passtest-passtest-passtest-passtest-passtest-passtest-passtest-passtest-passtest-passtest-passtest-passtest-passtest-passtest-passtest-passtest-passtest-passtest-passtest-passtest-passtest-passtest-passtest-passtest-passtest-passtest-passtest-passtest-passtest-passtest-passtest-passtest-passtest-passtest-passtest-passt-passtest-passtest-passtest-passtest-passtest-passtest-passtest-passtest-passtest-passtest-pass") |
| 44 | + |
| 45 | + hashed := FromBytes(data). |
| 46 | + FromPrivateKey([]byte(testPrikey)). |
| 47 | + WithOptions(opts). |
| 48 | + Sign() |
| 49 | + hashedData := hashed.ToBase64String() |
| 50 | + |
| 51 | + assertNoError(hashed.Error(), "ED521Sign-Sign") |
| 52 | + assertNotEmpty(hashedData, "ED521Sign-Sign") |
| 53 | + |
| 54 | + // === |
| 55 | + |
| 56 | + dehashed := FromBase64String(hashedData). |
| 57 | + FromPublicKey([]byte(testPubkey)). |
| 58 | + WithOptions(opts). |
| 59 | + Verify(data) |
| 60 | + dehashedVerify := dehashed.ToVerify() |
| 61 | + |
| 62 | + assertNoError(dehashed.Error(), "ED521Sign-Verify") |
| 63 | + assertTrue(dehashedVerify, "ED521Sign-Verify") |
| 64 | +} |
| 65 | + |
| 66 | +func Test_ED521Sign(t *testing.T) { |
| 67 | + ctx := "ase3ertygfa1" |
| 68 | + |
| 69 | + optses := []*Options{ |
| 70 | + &Options{ |
| 71 | + Hash: crypto.Hash(0), |
| 72 | + Context: ctx, |
| 73 | + Scheme: SchemeED521, |
| 74 | + }, |
| 75 | + &Options{ |
| 76 | + Hash: crypto.Hash(0), |
| 77 | + Context: ctx, |
| 78 | + Scheme: SchemeED521Ph, |
| 79 | + }, |
| 80 | + &Options{ |
| 81 | + Hash: crypto.Hash(0), |
| 82 | + Context: "", |
| 83 | + Scheme: SchemeED521, |
| 84 | + }, |
| 85 | + &Options{ |
| 86 | + Hash: crypto.Hash(0), |
| 87 | + Context: "", |
| 88 | + Scheme: SchemeED521Ph, |
| 89 | + }, |
| 90 | + } |
| 91 | + |
| 92 | + i := 1 |
| 93 | + for _, opts := range optses { |
| 94 | + t.Run(fmt.Sprintf("ED521 index %d", i), func(t *testing.T) { |
| 95 | + testED521Sign(t, opts) |
| 96 | + }) |
| 97 | + |
| 98 | + i += 1 |
| 99 | + } |
| 100 | +} |
| 101 | + |
| 102 | +func Test_CreateKey(t *testing.T) { |
| 103 | + assertNotEmpty := cryptobin_test.AssertNotEmptyT(t) |
| 104 | + assertNoError := cryptobin_test.AssertNoErrorT(t) |
| 105 | + |
| 106 | + obj := New().GenerateKey() |
| 107 | + |
| 108 | + objPriKey := obj.CreatePrivateKey() |
| 109 | + priKey := objPriKey.ToKeyString() |
| 110 | + |
| 111 | + assertNoError(objPriKey.Error(), "CreateKey-priKey") |
| 112 | + assertNotEmpty(priKey, "CreateKey-priKey") |
| 113 | + |
| 114 | + objPriKeyEn := obj.CreatePrivateKeyWithPassword("123", "AES256CBC", "SHA256") |
| 115 | + priKeyEn := objPriKeyEn.ToKeyString() |
| 116 | + |
| 117 | + assertNoError(objPriKeyEn.Error(), "CreateKey-priKeyEn") |
| 118 | + assertNotEmpty(priKeyEn, "CreateKey-priKeyEn") |
| 119 | + |
| 120 | + objPubKey := obj.CreatePublicKey() |
| 121 | + pubKey := objPubKey.ToKeyString() |
| 122 | + |
| 123 | + assertNoError(objPubKey.Error(), "CreateKey-pubKey") |
| 124 | + assertNotEmpty(pubKey, "CreateKey-pubKey") |
| 125 | + |
| 126 | + // t.Errorf("pri: %s, pub: %s, prien: %s \n", priKey, pubKey, priKeyEn) |
| 127 | +} |
| 128 | + |
| 129 | +func Test_CheckKeyPair(t *testing.T) { |
| 130 | + assertTrue := cryptobin_test.AssertTrueT(t) |
| 131 | + assertNoError := cryptobin_test.AssertNoErrorT(t) |
| 132 | + |
| 133 | + check := New(). |
| 134 | + FromPublicKey([]byte(testPubkey)). |
| 135 | + FromPrivateKey([]byte(testPrikey)) |
| 136 | + checkData := check.CheckKeyPair() |
| 137 | + |
| 138 | + assertNoError(check.Error(), "CheckKeyPair") |
| 139 | + assertTrue(checkData, "CheckKeyPair") |
| 140 | + |
| 141 | + // ========== |
| 142 | + |
| 143 | + checkEn := New(). |
| 144 | + FromPublicKey([]byte(testPubkeyEn)). |
| 145 | + FromPrivateKeyWithPassword([]byte(testPrikeyEn), "123") |
| 146 | + checkDataEn := checkEn.CheckKeyPair() |
| 147 | + |
| 148 | + assertNoError(checkEn.Error(), "CheckKeyPair-EnPri") |
| 149 | + assertTrue(checkDataEn, "CheckKeyPair-EnPri") |
| 150 | +} |
| 151 | + |
| 152 | +func Test_MakePublicKey(t *testing.T) { |
| 153 | + assertEqual := cryptobin_test.AssertEqualT(t) |
| 154 | + assertNoError := cryptobin_test.AssertNoErrorT(t) |
| 155 | + |
| 156 | + ed := New().FromPrivateKey([]byte(testPrikey)) |
| 157 | + newPubkey := ed.MakePublicKey(). |
| 158 | + CreatePublicKey(). |
| 159 | + ToKeyString() |
| 160 | + |
| 161 | + assertNoError(ed.Error(), "MakePublicKey") |
| 162 | + assertEqual(newPubkey, testPubkey, "MakePublicKey") |
| 163 | +} |
| 164 | + |
| 165 | +func Test_CheckKeyString(t *testing.T) { |
| 166 | + ed := New().GenerateKey() |
| 167 | + |
| 168 | + priString := ed.GetPrivateKeyString() |
| 169 | + pubString := ed.GetPublicKeyString() |
| 170 | + |
| 171 | + cryptobin_test.NotEmpty(t, priString) |
| 172 | + cryptobin_test.NotEmpty(t, pubString) |
| 173 | + |
| 174 | + pri := New(). |
| 175 | + FromPrivateKeyString(priString). |
| 176 | + GetPrivateKey() |
| 177 | + pub := New(). |
| 178 | + FromPublicKeyString(pubString). |
| 179 | + GetPublicKey() |
| 180 | + |
| 181 | + cryptobin_test.Equal(t, ed.GetPrivateKey(), pri) |
| 182 | + cryptobin_test.Equal(t, ed.GetPublicKey(), pub) |
| 183 | +} |
0 commit comments