Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Java > Other Java > BOOK: Beginning Cryptography with Java
|
BOOK: Beginning Cryptography with Java
This is the forum to discuss the Wrox book Beginning Cryptography with Java by David Hook; ISBN: 9780764596339
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Beginning Cryptography with Java section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old February 8th, 2010, 06:12 AM
Registered User
 
Join Date: Feb 2010
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default Handling large amount of data for signing and enveloping

Hi!


I'm writing a file signer/enveloper that does just fine when working with small files. The problem appears when I try to sign/envelop large files and the console gives me "failed: java.lang.OutOfMemoryError: Java heap space".

Anyone knows how to work this around? Here's a piece of my code for signing:

Code:
try {
             Certificate cert = keyStore.getCertificate(alias);
             PrivateKey key = (PrivateKey) keyStore.getKey(alias, null);
             Certificate[] chain = keyStore.getCertificateChain(alias);
             CertStore certStore = CertStore.getInstance("Collection",new CollectionCertStoreParameters(Arrays.asList(chain)));
             CMSSignedDataGenerator sdatagen = new CMSSignedDataGenerator();
             sdatagen.addSigner(key, (X509Certificate) cert, CMSSignedDataGenerator.DIGEST_SHA1);
             sdatagen.addCertificatesAndCRLs(certStore);            
             CMSSignedData sdata = sdatagen.generate(new CMSProcessableFile(arq, 16384), true, "SunPKCS11-iKey2032");
             Security.removeProvider(provider.getName());

Thank you!
 
Old February 8th, 2010, 02:12 PM
Registered User
 
Join Date: Feb 2010
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi,

Instead of using the CMSSignedDataGenerator I tried the CMSSignedDataStreamGenerator and it appears to handle large files just fine.

After the signature is generated, the content hash does not match the original content. What am I doing wrong? Here`s a piece of the new code:

Code:
int buff = 16384;
             byte[] buffer = new byte[buff];
             int unitsize = 0;
             long read = 0;
             long offset = arq.length();
             FileInputStream is = new FileInputStream(arq);
             FileOutputStream bOut = new FileOutputStream("teste.p7s");
             Certificate cert = keyStore.getCertificate(alias);
             PrivateKey key = (PrivateKey) keyStore.getKey(alias, null);
             Certificate[] chain = keyStore.getCertificateChain(alias);
             CertStore certStore = CertStore.getInstance("Collection",new CollectionCertStoreParameters(Arrays.asList(chain)));
             CMSSignedDataStreamGenerator gen = new CMSSignedDataStreamGenerator();
             gen.addSigner(key, (X509Certificate) cert, CMSSignedDataGenerator.DIGEST_SHA1, "SunPKCS11-iKey2032");
             gen.addCertificatesAndCRLs(certStore);
             OutputStream sigOut = gen.open(bOut,true);

             while (read < offset) {
                 unitsize = (int) (((offset - read) >= buff) ? buff : (offset - read));
                 is.read(buffer, 0, unitsize);
                 sigOut.write(buffer);
                 read += unitsize;
             }
             sigOut.close();
             bOut.close();
             is.close();
Thanks!
 
Old February 8th, 2010, 08:22 PM
dgh dgh is offline
Wrox Author
 
Join Date: Aug 2005
Posts: 206
Thanks: 0
Thanked 20 Times in 20 Posts
Default

Yes, the streaming API is the way to go.

The use of the generator looks basically correct, but the read loop is wrong - to start with it assumes buffer is full after each read. Try something like

Code:
int len = 0;

while ((len = is.read(buffer, 0, buffer.length)) > 0)
{
    sigOut.write(buffer, 0, len);
}
Regards,

David
 
Old February 9th, 2010, 05:58 AM
Registered User
 
Join Date: Feb 2010
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks David! It worked!

I've got another question:

Do you know if there's any way to retrieve the digest from the signed content using CMSSignedDataParser? Or do I have to retrieve the content and calculate the hash again?
 
Old February 9th, 2010, 07:16 AM
dgh dgh is offline
Wrox Author
 
Join Date: Aug 2005
Posts: 206
Thanks: 0
Thanked 20 Times in 20 Posts
Default

SignerInformation.getContentDigest() will return the digest value after verify is called.

Regards,

David





Similar Threads
Thread Thread Starter Forum Replies Last Post
array data amount lolotboy Forum and Wrox.com Feedback 1 June 29th, 2008 11:53 AM
using autosuggest with very large amount of info jfern Javascript 1 November 2nd, 2006 07:00 AM
How display large amount of dynamic text relaytest49 ASP.NET 2.0 Professional 2 October 6th, 2006 02:19 PM
Managing large amount of data at a time pandu345 Java Databases 0 May 19th, 2006 04:51 PM
Help with large amount of text and page breaks rwodabek BOOK: Professional SQL Server Reporting Services ISBN: 0-7645-6878-7 1 May 12th, 2006 07:49 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.