Created
June 14, 2016 11:33
-
-
Save tsusanka/e449f396ccba8867c73578fd0c478010 to your computer and use it in GitHub Desktop.
einf.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.tsusanka.dsa; | |
import java.math.BigInteger; | |
class DSA | |
{ | |
private BigInteger alpha; | |
private BigInteger beta; | |
private BigInteger p; | |
private BigInteger q; | |
DSA(BigInteger alpha, BigInteger beta, BigInteger p, BigInteger q) | |
{ | |
this.alpha = alpha; | |
this.beta = beta; | |
this.p = p; | |
this.q = q; | |
} | |
Signature sign(BigInteger key, BigInteger ephemeral, BigInteger hash) | |
{ | |
BigInteger r, s; | |
r = alpha.modPow(ephemeral, p).mod(q); | |
s = (hash.add(key.multiply(r))).mod(q).multiply(ephemeral.modInverse(q)).mod(q); | |
return new Signature(r, s); | |
} | |
boolean verify(Signature signature, BigInteger hash) | |
{ | |
BigInteger w, u1, u2, v; | |
w = signature.s.modInverse(q); | |
u1 = w.multiply(hash).mod(q); | |
u2 = w.multiply(signature.r).mod(q); | |
v = (alpha.modPow(u1, p).multiply(beta.modPow(u2, p))).mod(p).mod(q); | |
return v.equals(signature.r); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment