KeyFactory fact = KeyFactory.getInstance("RSA", "BC");
PrivateKey privKey = fact.generatePrivate(privKeySpec);
PublicKey pubKey = fact.generatePublic(pubKeySpec);
Signature s = Signature.getInstance("SHA1withRSA/PSS", "BC");
s.initSign(privKey, new FixedRandom(slt1a));
s.update(msg1a);
byte[] sig = s.sign();
if (!arrayEquals(sig1a, sig))
{
fail("PSS Sign test expected " + new String(Hex.encode(sig1a)) + " got " + new String(Hex.encode(sig)));
}
s = Signature.getInstance("SHA1withRSAandMGF1", "BC");
s.initVerify(pubKey);
s.update(msg1a);
if (!s.verify(sig1a))
{
fail("SHA1 signature verification failed");
}
s = Signature.getInstance("SHA1withRSAandMGF1", "BC");
s.setParameter(PSSParameterSpec.DEFAULT);
s.initVerify(pubKey);
s.update(msg1a);
if (!s.verify(sig1a))
{
fail("SHA1 signature verification with default parameters failed");
}
AlgorithmParameters pss = s.getParameters();
if (!arrayEquals(pss.getEncoded(), new byte[] { 0x30, 0x00 }))
{
fail("failed default encoding test.");
}
s = Signature.getInstance("SHA256withRSA/PSS", "BC");
s.initSign(privKey, new FixedRandom(slt1a));
s.update(msg1a);
sig = s.sign();
pss = s.getParameters();
if (!arrayEquals(sig1b, sig))
{
fail("PSS Sign test expected " + new String(Hex.encode(sig1b)) + " got " + new String(Hex.encode(sig)));
}
s = Signature.getInstance("SHA256withRSAandMGF1", "BC");
s.setParameter(pss.getParameterSpec(PSSParameterSpec.class));
s.initVerify(pubKey);
s.update(msg1a);
if (!s.verify(sig1b))
{
fail("SHA256 signature verification failed");
}
//
// 512 test -with zero salt length
//
s = Signature.getInstance("SHA512withRSAandMGF1", "BC");
s.setParameter(new PSSParameterSpec("SHA-512", "MGF1", new MGF1ParameterSpec("SHA-512"), 0, 1));
s.initSign(privKey);
s.update(msg1a);
sig = s.sign();
pss = s.getParameters();
if (!arrayEquals(sig1c, sig))
{
fail("PSS Sign test expected " + new String(Hex.encode(sig1c)) + " got " + new String(Hex.encode(sig)));
}
s = Signature.getInstance("SHA512withRSAandMGF1", "BC");
s.setParameter(pss.getParameterSpec(PSSParameterSpec.class));
s.initVerify(pubKey);
s.update(msg1a);
if (!s.verify(sig1c))
{
fail("SHA512 signature verification failed");
}
SecureRandom random = new SecureRandom();