(t *testing.T)
| 8 | ) |
| 9 | |
| 10 | func TestDSA(t *testing.T) { |
| 11 | tests := []struct { |
| 12 | name string |
| 13 | message string |
| 14 | alter bool |
| 15 | want bool |
| 16 | }{ |
| 17 | { |
| 18 | name: "valid signature", |
| 19 | message: "Hello, world!", |
| 20 | alter: false, |
| 21 | want: true, |
| 22 | }, |
| 23 | { |
| 24 | name: "invalid signature", |
| 25 | message: "Hello, world!", |
| 26 | alter: true, |
| 27 | want: false, |
| 28 | }, |
| 29 | } |
| 30 | // Generate a DSA key pair |
| 31 | dsaInstance := dsa.New() |
| 32 | pubKey := dsaInstance.GetPublicKey() |
| 33 | params := dsaInstance.GetParameters() |
| 34 | privKey := dsaInstance.GetPrivateKey() |
| 35 | |
| 36 | for _, tt := range tests { |
| 37 | t.Run(tt.name, func(t *testing.T) { |
| 38 | |
| 39 | // Sign the message |
| 40 | r, s := dsa.Sign( |
| 41 | []byte(tt.message), |
| 42 | params.P, |
| 43 | params.Q, |
| 44 | params.G, |
| 45 | privKey, |
| 46 | ) |
| 47 | |
| 48 | if tt.alter { |
| 49 | // Alter the signature |
| 50 | r.Add(r, big.NewInt(1)) |
| 51 | } |
| 52 | |
| 53 | // Verify the signature |
| 54 | if got := dsa.Verify( |
| 55 | []byte(tt.message), |
| 56 | r, |
| 57 | s, |
| 58 | params.P, |
| 59 | params.Q, |
| 60 | params.G, |
| 61 | pubKey, |
| 62 | ); got != tt.want { |
| 63 | t.Errorf("got %v, want %v", got, tt.want) |
| 64 | } |
| 65 | }) |
| 66 | } |
| 67 | } |
nothing calls this directly
no test coverage detected