BcRsaSsaPssSignerFactory extends AbstractBcPssSignerFactory { @Override public Signer getInstance(boolean forSigning, CipherParameters parameters, AlgorithmIdentifier algId) { if (!algId.getAlgorithm().equals(PKCSObjectIdentifiers.id_RSASSA_PSS)) { throw new IllegalArgumentException("Incompatible algorithm for this signer: " + algId.getAlgorithm().getId()); } ASN1Encodable algParams = algId.getParameters(); if (DERNull.INSTANCE.equals(algParams)) { return getInstance(forSigning, parameters); } else { RSASSAPSSparams pssParams = RSASSAPSSparams.getInstance(algId.getParameters()); if (parameters instanceof AsymmetricKeyParameters) { return getInstance(forSigning, new PssSignerParameters((AsymmetricKeyParameters) parameters, pssParams.getHashAlgorithm().getAlgorithm().getId(), AlgorithmIdentifier .getInstance(pssParams.getMaskGenAlgorithm().getParameters()).getAlgorithm().getId(), pssParams.getSaltLength().intValue(), pssParams.getTrailerField().intValue())); } } throw new UnsupportedOperationException(PSS_PARAMS_ERROR + parameters.getClass().getName()); }  @Override Signer getInstance(boolean forSigning, CipherParameters parameters, AlgorithmIdentifier algId);  }
@Test public void testDefaultSignatureVerification() throws Exception { runTestSignatureVerification( factory.getInstance(true, privateKey), factory.getInstance(false, publicKey) ); }
@Test public void testSha224SignatureVerification() throws Exception { runTestSignatureVerification( factory.getInstance(true, new PssSignerParameters(privateKey, "SHA-224", -1)), factory.getInstance(false, new PssSignerParameters(publicKey, "SHA-224", -1)) ); }
@Test public void testSha256SignatureVerification() throws Exception { runTestSignatureVerification( factory.getInstance(true, new PssSignerParameters(privateKey, "SHA-256", -1)), factory.getInstance(false, new PssSignerParameters(publicKey, "SHA-256", -1)) ); }
@Test public void testSha384SignatureVerification() throws Exception { runTestSignatureVerification( factory.getInstance(true, new PssSignerParameters(privateKey, "SHA-384", -1)), factory.getInstance(false, new PssSignerParameters(publicKey, "SHA-384", -1)) ); }
@Test public void testSha512SignatureVerification() throws Exception { runTestSignatureVerification( factory.getInstance(true, new PssSignerParameters(privateKey, "SHA-512", -1)), factory.getInstance(false, new PssSignerParameters(publicKey, "SHA-512", -1)) ); }
@Test public void testEncodedDefaultSignatureVerification() throws Exception { Signer signer = factory.getInstance(true, privateKey); Signer verifier = factory.getInstance(false, publicKey, signer.getEncoded()); runTestSignatureVerification(signer, verifier); }
@Test public void testEncodedSha224SignatureVerification() throws Exception { Signer signer = factory.getInstance(true, new PssSignerParameters(privateKey,"SHA-224", -1)); Signer verifier = factory.getInstance(false, publicKey, signer.getEncoded()); runTestSignatureVerification(signer, verifier); }
@Test public void testEncodedSha256SignatureVerification() throws Exception { Signer signer = factory.getInstance(true, new PssSignerParameters(privateKey,"SHA-256", -1)); Signer verifier = factory.getInstance(false, publicKey, signer.getEncoded()); runTestSignatureVerification(signer, verifier); }
@Test public void testEncodedSha384SignatureVerification() throws Exception { Signer signer = factory.getInstance(true, new PssSignerParameters(privateKey,"SHA-384", -1)); Signer verifier = factory.getInstance(false, publicKey, signer.getEncoded()); runTestSignatureVerification(signer, verifier); }
@Test public void testEncodedSha512SignatureVerification() throws Exception { Signer signer = factory.getInstance(true, new PssSignerParameters(privateKey,"SHA-512", -1)); Signer verifier = factory.getInstance(false, publicKey, signer.getEncoded()); runTestSignatureVerification(signer, verifier); }
@Test public void testProgressiveSignatureVerification() throws Exception { Signer signer = factory.getInstance(true, privateKey); progressiveUpdateSignature(signer, text, 17); byte[] signature = signer.generate(); Signer verifier = factory.getInstance(true, publicKey); progressiveUpdateSignature(verifier, text, 15); assertTrue(verifier.verify(signature)); }
@Test public void testPartialBufferVerification() throws Exception { Signer signer = factory.getInstance(true, privateKey); byte[] source = new byte[text.length * 2]; System.arraycopy(text, 0, source, text.length - 10, text.length); byte[] signature = signer.generate(source, text.length - 10, text.length); byte[] sign = new byte[signature.length * 2]; System.arraycopy(signature, 0, sign, signature.length - 5, signature.length); Signer verifier = factory.getInstance(true, publicKey); assertTrue( verifier.verify(sign, signature.length - 5, signature.length, source, text.length - 10, text.length)); }
@Test public void testStreamSignatureVerification() throws Exception { ByteArrayInputStream bais = new ByteArrayInputStream(text); Signer signer = factory.getInstance(true, privateKey); InputStream input = signer.getInputStream(bais); byte[] buf = new byte[text.length]; assertThat(readAll(input, buf), equalTo(text.length)); assertThat(buf, equalTo(text)); byte[] signature = signer.generate(); Signer verifier = factory.getInstance(false, publicKey); OutputStream output = verifier.getOutputStream(); output.write(text); assertTrue(verifier.verify(signature)); }