vulnerabilities = new ArrayList(entry.getValue().values());
vulnerabilities.sort((v1, v2) -> {
return v1.getCve().getId().compareTo(v2.getCve().getId());
@@ -291,16 +302,62 @@ private Integer processRequest(NvdCveClientBuilder builder, CacheProperties prop
properties.set("lastModifiedDate." + entry.getKey(), timestamp);
CveApiJson20 data = new CveApiJson20(vulnerabilities.size(), 0, vulnerabilities.size(), format, version,
timestamp, vulnerabilities);
+ MessageDigest md;
+ try {
+ md = MessageDigest.getInstance("SHA-256");
+ } catch (NoSuchAlgorithmException e) {
+ throw new CacheException("Unable to calculate sha256 checksum", e);
+ }
+ long byteCount = 0;
try (FileOutputStream fileOutputStream = new FileOutputStream(file);
- GZIPOutputStream gzipOutputStream = new GZIPOutputStream(fileOutputStream);) {
- objectMapper.writeValue(gzipOutputStream, data);
+ GZIPOutputStream gzipOutputStream = new GZIPOutputStream(fileOutputStream);
+ DigestOutputStream digestOutputStream = new DigestOutputStream(gzipOutputStream, md);
+ CountingOutputStream countingOutputStream = new CountingOutputStream(digestOutputStream)) {
+ objectMapper.writeValue(countingOutputStream, data);
+ byteCount = countingOutputStream.getByteCount();
} catch (IOException ex) {
throw new CacheException("Unable to write cached data: " + file, ex);
}
+ String checksum = getHex(md.digest());
+ try (FileOutputStream fileOutputStream = new FileOutputStream(meta);
+ OutputStreamWriter osw = new OutputStreamWriter(fileOutputStream, "UTF-8");
+ PrintWriter writer = new PrintWriter(osw)) {
+ final String lmd = DateTimeFormatter.ISO_DATE_TIME.format(timestamp);
+ writer.println("lastModifiedDate:" + lmd);
+ writer.println("size:" + byteCount);
+ writer.println("gzSize:" + file.length());
+ writer.println("sha256:" + checksum);
+ } catch (IOException ex) {
+ throw new CacheException("Unable to write cached meta-data: " + file, ex);
+ }
}
return 0;
}
+ /**
+ *
+ * Converts a byte array into a hex string.
+ *
+ *
+ *
+ * This method was copied from
+ * http://www.rgagnon.com/javadetails/java-0596.html
+ *
+ *
+ * @param raw a byte array
+ * @return the hex representation of the byte array
+ */
+ public static String getHex(byte[] raw) {
+ if (raw == null) {
+ return null;
+ }
+ final StringBuilder hex = new StringBuilder(2 * raw.length);
+ for (final byte b : raw) {
+ hex.append(HEXES.charAt((b & 0xF0) >> 4)).append(HEXES.charAt(b & 0x0F));
+ }
+ return hex.toString();
+ }
+
private void collectCves(HashMap> cves,
Collection vulnerabilities) {
for (DefCveItem item : vulnerabilities) {