Search code examples
javahashbouncycastletrusted-timestamp

Timestamp request with file hash already generated in client


I need to make a timpestamp request to a tsa of a large data file and so i am generating hash in client using javscript crypto-js.

The problem comes when later in java i try to make the request. Apparently the method TimeStampRequestGenerator.generate needs a byte[] parameter that in examples i can se that is a MessageDigest object generated from the content of the file and i can't find the way to use only the hash already generated.

Is it possible to make a request using only the hash of the file already generated ?

Thanks


Solution

  • After hard testing, i have found the solution.

    The SHA-256 hash generated in javascript can be used directly in bouncyclaste after some type conversion as follows:

    byte[] decodedHex = Hex.decodeHex(digest.toCharArray());
    

    so you can use it as a normal

    java.security.MessageDigest
    

    when they are both converted to

    byte[]
    

    full code here:

    // Get hash code as hexadecimal string (generated by crypto-js)
    String digest = servletRequest.getParameter("digest");
    
    // hexadecimal to string decoder
    byte[] decodedHex = Hex.decodeHex(digest.toCharArray());
    
    // Timestamp request
    TimeStampRequestGenerator reqgen = new TimeStampRequestGenerator();
    TimeStampRequest req = reqgen.generate(TSPAlgorithms.SHA256, decodedHex);
    byte request[] = req.getEncoded();
    ...