X-Git-Url: https://git.argeo.org/?a=blobdiff_plain;f=org.argeo.cms%2Fsrc%2Forg%2Fargeo%2Fcms%2Fsecurity%2FChecksumFactory.java;h=7344f01bc25ee6ebda7b7c6cd4f5dd6c16ce7844;hb=3c1cdc594d954520b14646102b366290bdad58c7;hp=c5b4e9ecc65c0437fb1f6f2e44b6667153987a6c;hpb=088c1b517a543e935d8ab65c3b2fd2d0269b551d;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..7344f01bc 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,10 +11,10 @@ import java.nio.file.Paths; import java.nio.file.SimpleFileVisitor; import java.nio.file.attribute.BasicFileAttributes; import java.security.MessageDigest; +import java.security.NoSuchAlgorithmException; +import java.util.Base64; import java.util.zip.Checksum; -import org.argeo.cms.CmsException; - /** Allows to fine tune how files are read. */ public class ChecksumFactory { private int regionSize = 10 * 1024 * 1024; @@ -29,8 +27,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 +38,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,16 +74,14 @@ 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; } } - } catch (Exception e) { - throw new CmsException("Cannot digest " + path, e); + } catch (NoSuchAlgorithmException | IOException e) { + throw new IllegalStateException("Cannot digest " + path, e); } } @@ -112,8 +102,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); @@ -123,8 +112,8 @@ public class ChecksumFactory { cursor = cursor + regionSize; } return crc.getValue(); - } catch (Exception e) { - throw new CmsException("Cannot checksum " + path, e); + } catch (IOException e) { + throw new IllegalStateException("Cannot checksum " + path, e); } finally { long duration = System.currentTimeMillis() - begin; System.out.println(duration / 1000 + "s"); @@ -139,9 +128,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 +142,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); + } }