X-Git-Url: https://git.argeo.org/?a=blobdiff_plain;f=org.argeo.cms%2Fsrc%2Forg%2Fargeo%2Fcms%2Fsecurity%2FChecksumFactory.java;h=69e8a08fda9b616885d4870e7c37a27530d3733d;hb=810aecacb19916bade7e4bcfcbbb54c301f672df;hp=c5b4e9ecc65c0437fb1f6f2e44b6667153987a6c;hpb=ca59ec5bdf16364159d8d826306c90762192e12c;p=lgpl%2Fargeo-commons.git diff --git a/org.argeo.cms/src/org/argeo/cms/security/ChecksumFactory.java b/org.argeo.cms/src/org/argeo/cms/security/ChecksumFactory.java index c5b4e9ecc..69e8a08fd 100644 --- a/org.argeo.cms/src/org/argeo/cms/security/ChecksumFactory.java +++ b/org.argeo.cms/src/org/argeo/cms/security/ChecksumFactory.java @@ -1,7 +1,5 @@ package org.argeo.cms.security; -import static javax.xml.bind.DatatypeConverter.printBase64Binary; - import java.io.IOException; import java.math.BigInteger; import java.nio.MappedByteBuffer; @@ -13,6 +11,7 @@ import java.nio.file.Paths; import java.nio.file.SimpleFileVisitor; import java.nio.file.attribute.BasicFileAttributes; import java.security.MessageDigest; +import java.util.Base64; import java.util.zip.Checksum; import org.argeo.cms.CmsException; @@ -29,8 +28,7 @@ public class ChecksumFactory { Files.walkFileTree(path, new SimpleFileVisitor() { @Override - public FileVisitResult visitFile(Path file, - BasicFileAttributes attrs) throws IOException { + public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { if (!Files.isDirectory(file)) { byte[] digest = digest(file, algo); md.update(digest); @@ -41,25 +39,20 @@ public class ChecksumFactory { }); byte[] digest = md.digest(); long duration = System.currentTimeMillis() - begin; - System.out.println(printBase64Binary(digest) + " " + path - + " (" + duration / 1000 + "s)"); + System.out.println(printBase64Binary(digest) + " " + path + " (" + duration / 1000 + "s)"); return digest; } else { long begin = System.nanoTime(); long length = -1; - try (FileChannel channel = (FileChannel) Files - .newByteChannel(path);) { + try (FileChannel channel = (FileChannel) Files.newByteChannel(path);) { length = channel.size(); long cursor = 0; while (cursor < length) { - long effectiveSize = Math.min(regionSize, length - - cursor); - MappedByteBuffer mb = channel.map( - FileChannel.MapMode.READ_ONLY, cursor, - effectiveSize); + long effectiveSize = Math.min(regionSize, length - cursor); + MappedByteBuffer mb = channel.map(FileChannel.MapMode.READ_ONLY, cursor, effectiveSize); // md.update(mb); byte[] buffer = new byte[1024]; - while (mb.hasRemaining()){ + while (mb.hasRemaining()) { mb.get(buffer); md.update(buffer); } @@ -82,11 +75,9 @@ public class ChecksumFactory { } byte[] digest = md.digest(); long duration = System.nanoTime() - begin; - System.out.println(printBase64Binary(digest) + " " - + path.getFileName() + " (" + duration / 1000000 - + "ms, " + (length / 1024) + "kB, " - + (length / (duration / 1000000)) * 1000 - / (1024 * 1024) + " MB/s)"); + System.out.println(printBase64Binary(digest) + " " + path.getFileName() + " (" + duration / 1000000 + + "ms, " + (length / 1024) + "kB, " + (length / (duration / 1000000)) * 1000 / (1024 * 1024) + + " MB/s)"); return digest; } } @@ -112,8 +103,7 @@ public class ChecksumFactory { long cursor = 0; while (cursor < length) { long effectiveSize = Math.min(regionSize, length - cursor); - MappedByteBuffer mb = channel.map( - FileChannel.MapMode.READ_ONLY, cursor, effectiveSize); + MappedByteBuffer mb = channel.map(FileChannel.MapMode.READ_ONLY, cursor, effectiveSize); int nGet; while (mb.hasRemaining()) { nGet = Math.min(mb.remaining(), bufferSize); @@ -139,9 +129,8 @@ public class ChecksumFactory { if (args.length > 0) { path = Paths.get(args[0]); } else { - path = Paths - .get("/home/mbaudier/Downloads/torrents/CentOS-7-x86_64-DVD-1503-01/" - + "CentOS-7-x86_64-DVD-1503-01.iso"); + path = Paths.get("/home/mbaudier/Downloads/torrents/CentOS-7-x86_64-DVD-1503-01/" + + "CentOS-7-x86_64-DVD-1503-01.iso"); } // long adler = cf.checksum(path, new Adler32()); // System.out.format("Adler=%d%n", adler); @@ -154,4 +143,8 @@ public class ChecksumFactory { // String sha1 = printBase64Binary(cf.digest(path, "SHA1")); // System.out.format("SHA1=%s%n", sha1); } + + private static String printBase64Binary(byte[] arr) { + return Base64.getEncoder().encodeToString(arr); + } }