Sign is signature generation for DSA 1. Choose a random integer k from the range [1, q-1] 2. Compute r = (g^k mod p) mod q 3. Compute s = (k^-1 * (H(m) + x*r)) mod q
(m []byte, p, q, g, x *big.Int)
| 123 | // 2. Compute r = (g^k mod p) mod q |
| 124 | // 3. Compute s = (k^-1 * (H(m) + x*r)) mod q |
| 125 | func Sign(m []byte, p, q, g, x *big.Int) (r, s *big.Int) { |
| 126 | // 1. Choose a random integer k from the range [1, q-1] |
| 127 | k, err := rand.Int(rand.Reader, new(big.Int).Sub(q, big.NewInt(1))) |
| 128 | if err != nil { |
| 129 | panic(err) |
| 130 | } |
| 131 | |
| 132 | // 2. Compute r = (g^k mod p) mod q |
| 133 | r = new(big.Int).Exp(g, k, p) |
| 134 | r.Mod(r, q) |
| 135 | |
| 136 | // 3. Compute s = (k^-1 * (H(m) + x*r)) mod q |
| 137 | h := new(big.Int).SetBytes(m) // This should be the hash of the message |
| 138 | s = new(big.Int).ModInverse(k, q) // k^-1 mod q |
| 139 | s.Mul( |
| 140 | s, |
| 141 | new(big.Int).Add( // (H(m) + x*r) |
| 142 | h, |
| 143 | new(big.Int).Mul(x, r), |
| 144 | ), |
| 145 | ) |
| 146 | s.Mod(s, q) // mod q |
| 147 | |
| 148 | return r, s |
| 149 | } |
| 150 | |
| 151 | // Verify is signature verification for DSA |
| 152 | // 1. Compute w = s^-1 mod q |