0

In my Spring Boot app, I am generating a Pdf from Html content and store it on cloud and return the generated file to the front-end. As the data that is used for generation may change in the next time, I should delete the generated file from the cloud. Or, I can generate a key that is created using a uuid and Html data. With the help of this, at first I can check if the generated key is already present in the cloud and retrieve that data from cloud if it is present. Otherwise generate Pdf.

I think to use some basic approach as shown below:

public int hashCode(UUID uuid, String content) {
    StringBuilder builder = new StringBuilder();
    builder.append(uuid);
    builder.append(content);
    return builder.toString().hashCode();
}

On the other hand, there may be some better solution as shown below:

private static final int NUM_BITS = 256;
private static final int RADIX = 36;
private static final SecureRandom secureRandom = new SecureRandom();

private static String generateKey() {
    return new BigInteger(NUM_BITS, secureRandom).toString(RADIX);
}

However, I don't know if I can pass uuid and html content to this method and use them instead of some parameter e.g. NUM_BITS.

1. As I generate pdf for a specific product, I think it is good idea to use uuid of that product for creating hash key besides generated Pdf content. Am I wrong?

2. Which one is the most proper way to create a hashkey? If the approaches above is not a proper ones, what would you suggest?

I might also consider using How to generate a random alpha-numeric string, but not sure which one is the most proper way for my situation?

  • I just need to compare generated html, not pdf. What is your suggestion? –  Jul 26 '22 at 11:01

1 Answers1

1

You can easily use the Objects.hash() method (since Java 1.7) which accepts a variable number of arguments to generate a hash based on multiple inputs.

Objects.hash(uuid, content);

If you want a unique file per product, add the UUID of the product to the hash as well as the HTML content. I think that using the generated pdf content is not a good idea, as I believe each time you generate a PDF some metadata changes in the PDF causing it to have a different hash each time defeating your purpose.

Yves V.
  • 775
  • 1
  • 7
  • 20
  • Thanks a lot. Regarding to "using the generated pdf content", actually I will generate html string and use it. Then, without changing the template, the generated html string would have the same data and metadata. If the template (used for generating html) or style is changed, of course I want a new file with this style will be generated. Does not it? Is that still not good idea? What would you suggest? –  Jul 26 '22 at 08:50
  • I was referring to generated PDF content. Typically, a generated PDF will have metadata such as date/time of creation, which would change. Hashing the html content or template is not a problem – Yves V. Jul 26 '22 at 08:55
  • Thanks a lot, I can use this approach. But I cannot convert to String this: `Objects.hash(uuid, content)`, how can I convert to String? –  Jul 26 '22 at 11:00
  • You mean how to convert an int to a String? Simply add it to "" ? – Yves V. Jul 26 '22 at 11:01
  • Thanks amigo, I will try and ask you if I need further help. Voted up and marked as answer ;) –  Jul 28 '22 at 07:02