InMemoryKsiSignatureFactory implements KSISignatureFactory { public KSISignature createSignature(InputStream input) throws KSIException { TLVInputStream tlvInput = new TLVInputStream(input); try { return createSignature(tlvInput.readElement(), extendingAllowed, null); } catch (IOException e) { throw new KSIException("Reading signature data from input stream failed", e); } finally { Util.closeQuietly(tlvInput); } } InMemoryKsiSignatureFactory();  InMemoryKsiSignatureFactory(KSISignatureComponentFactory signatureComponentFactory);  InMemoryKsiSignatureFactory(ContextAwarePolicy policy, KSISignatureComponentFactory signatureComponentFactory); @Deprecated  InMemoryKsiSignatureFactory(Policy policy, PublicationsFileClientAdapter publicationsFileClientAdapter,
                                       KSIExtendingService extendingService, boolean extendingAllowed,
                                       KSISignatureComponentFactory signatureComponentFactory); @Deprecated  InMemoryKsiSignatureFactory(Policy policy, PublicationsFileClientAdapter publicationsFileClientAdapter,
                                       KSIExtenderClient extenderClient, boolean extendingAllowed,
                                       KSISignatureComponentFactory signatureComponentFactory); @Deprecated  InMemoryKsiSignatureFactory(Policy policy, PublicationsFileClientAdapter publicationsFileClientAdapter,
                                       KSIExtenderClient extenderClient, boolean extendingAllowed,  PduFactory pduFactory,
                                       KSISignatureComponentFactory signatureComponentFactory); KSISignature createSignature(InputStream input); KSISignature createSignature(TLVElement element, DataHash inputHash); KSISignature createSignature(TLVElement element, DataHash inputHash, long level); KSISignature createSignature(List<AggregationHashChain> aggregationHashChains,
                                        CalendarHashChain calendarChain, CalendarAuthenticationRecord calendarAuthenticationRecord,
                                        PublicationRecord signaturePublicationRecord, RFC3161Record rfc3161Record); KSISignature createSignature(KSISignature signature, AggregationHashChain aggregationHashChain,
                                        DataHash originalInputHash);  }
@Test public void testCreateValidKsiSignature_Ok() throws Exception { KSISignature signature = signatureFactory.createSignature(TestUtil.loadTlv(SIGNATURE_2017_03_14), null); Assert.assertNotNull(signature); }
@Test(expectedExceptions = AggregationHashChainPrependingException.class, expectedExceptionsMessageRegExp = "The aggregation hash chain cannot be added as lowest level chain. " + "Its output level .* is bigger than level correction of the first link of the first aggregation hash chain of the base signature .*") public void testCreateSignatureWithInvalidChainHeight_throwsKSIException() throws Exception { AggregationHashChain chain = new InMemoryAggregationHashChain(loadTlv(SIGNATURE_AGGREGATION_HASH_CHAIN_OK) .getFirstChildElement(InMemoryAggregationHashChain.ELEMENT_TYPE)); signatureFactory.createSignature(TestUtil.loadSignature(SIGNATURE_2017_03_14), chain, null); }
@Test(expectedExceptions = AggregationHashChainPrependingException.class, expectedExceptionsMessageRegExp = "The aggregation hash chain cannot be added as lowest level chain. " + "Its output hash .* does not match base signature input hash .*") public void testCreateSignatureWithInvalidOutputHash_throwsKSIException() throws Exception { AggregationHashChain chain = new InMemoryAggregationHashChain(loadTlv(AGGREGATION_HASH_CHAIN_WITH_HEIGHT_2)); KSISignature signature = TestUtil.loadSignature(SIGNATURE_WITH_LEVEL_CORRECTION_3); signatureFactory.createSignature(signature, chain, null); }
@Test(expectedExceptions = InvalidSignatureContentException.class, expectedExceptionsMessageRegExp = "Signature .* is invalid: GEN_04.*Wrong input hash algorithm.*") public void testCreateSignatureWithInvalidInputHashAlgorithm_ThrowsInvalidSignatureContentException() throws Exception { signatureFactory.createSignature(TestUtil.loadTlv(SIGNATURE_2017_03_14), new DataHash(HashAlgorithm.SHA1, new byte[20])); }
@Test(expectedExceptions = InvalidSignatureContentException.class, expectedExceptionsMessageRegExp = "Signature .* is invalid: GEN_01.*Wrong document.*") public void testCreateSignatureWithInvalidInputHash_ThrowsInvalidSignatureContentException() throws Exception { signatureFactory.createSignature(TestUtil.loadTlv(SIGNATURE_2017_03_14), new DataHash(HashAlgorithm.SHA2_256, new byte[32])); }
@Test(expectedExceptions = InvalidSignatureContentException.class, expectedExceptionsMessageRegExp = "Signature .* is invalid: INT_07.*") public void testCreateSignatureFromInvalidComponents_ThrowsInvalidSignatureContentException() throws Exception { KSISignature signature = TestUtil.loadSignature(SIGNATURE_2017_03_14); SignaturePublicationRecord publicationRecord = signatureComponentFactory.createPublicationRecord(new PublicationData(PUBLICATION_STRING), null, null); signatureFactory.createSignature(asList(signature.getAggregationHashChains()), signature.getCalendarHashChain(), signature.getCalendarAuthenticationRecord(), publicationRecord, null); }