@Test public void testParse() { assertEquals(new Checksum(HashAlgorithm.md5, "d41d8cd98f00b204e9800998ecf8427e"), Checksum.parse("d41d8cd98f00b204e9800998ecf8427e")); assertEquals(new Checksum(HashAlgorithm.sha1, "da39a3ee5e6b4b0d3255bfef95601890afd80709"), Checksum.parse("da39a3ee5e6b4b0d3255bfef95601890afd80709")); assertEquals(new Checksum(HashAlgorithm.sha256, "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"), Checksum.parse("e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855")); assertEquals(Checksum.NONE, Checksum.parse("da39a3ee5e6b4b0d3255bfef95601890afd80709-2")); assertEquals(Checksum.NONE, Checksum.parse("")); assertEquals(Checksum.NONE, Checksum.parse(null)); assertEquals(new Checksum(HashAlgorithm.sha512, "cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce47d0d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e"), Checksum.parse("cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce47d0d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e")); assertEquals(new Checksum(HashAlgorithm.crc32, "d202ef8d"), Checksum.parse("d202ef8d")); }
public static Checksum parse(final String hash) { if(StringUtils.isBlank(hash)) { return Checksum.NONE; } switch(hash.length()) { case 8: if(hash.matches("[a-fA-F0-9]{8}")) { return new Checksum(HashAlgorithm.crc32, hash); } break; case 32: if(hash.matches("[a-fA-F0-9]{32}")) { return new Checksum(HashAlgorithm.md5, hash); } break; case 40: if(hash.matches("[a-fA-F0-9]{40}")) { return new Checksum(HashAlgorithm.sha1, hash); } break; case 64: if(hash.matches("[A-Fa-f0-9]{64}")) { return new Checksum(HashAlgorithm.sha256, hash); } break; case 128: if(hash.matches("[A-Fa-f0-9]{128}")) { return new Checksum(HashAlgorithm.sha512, hash); } break; default: log.warn(String.format("Failure to detect algorithm for checksum %s", hash)); } return Checksum.NONE; }
Checksum { public static Checksum parse(final String hash) { if(StringUtils.isBlank(hash)) { return Checksum.NONE; } switch(hash.length()) { case 8: if(hash.matches("[a-fA-F0-9]{8}")) { return new Checksum(HashAlgorithm.crc32, hash); } break; case 32: if(hash.matches("[a-fA-F0-9]{32}")) { return new Checksum(HashAlgorithm.md5, hash); } break; case 40: if(hash.matches("[a-fA-F0-9]{40}")) { return new Checksum(HashAlgorithm.sha1, hash); } break; case 64: if(hash.matches("[A-Fa-f0-9]{64}")) { return new Checksum(HashAlgorithm.sha256, hash); } break; case 128: if(hash.matches("[A-Fa-f0-9]{128}")) { return new Checksum(HashAlgorithm.sha512, hash); } break; default: log.warn(String.format("Failure to detect algorithm for checksum %s", hash)); } return Checksum.NONE; } }
Checksum { public static Checksum parse(final String hash) { if(StringUtils.isBlank(hash)) { return Checksum.NONE; } switch(hash.length()) { case 8: if(hash.matches("[a-fA-F0-9]{8}")) { return new Checksum(HashAlgorithm.crc32, hash); } break; case 32: if(hash.matches("[a-fA-F0-9]{32}")) { return new Checksum(HashAlgorithm.md5, hash); } break; case 40: if(hash.matches("[a-fA-F0-9]{40}")) { return new Checksum(HashAlgorithm.sha1, hash); } break; case 64: if(hash.matches("[A-Fa-f0-9]{64}")) { return new Checksum(HashAlgorithm.sha256, hash); } break; case 128: if(hash.matches("[A-Fa-f0-9]{128}")) { return new Checksum(HashAlgorithm.sha512, hash); } break; default: log.warn(String.format("Failure to detect algorithm for checksum %s", hash)); } return Checksum.NONE; } Checksum(final HashAlgorithm algorithm, final String hash); }
Checksum { public static Checksum parse(final String hash) { if(StringUtils.isBlank(hash)) { return Checksum.NONE; } switch(hash.length()) { case 8: if(hash.matches("[a-fA-F0-9]{8}")) { return new Checksum(HashAlgorithm.crc32, hash); } break; case 32: if(hash.matches("[a-fA-F0-9]{32}")) { return new Checksum(HashAlgorithm.md5, hash); } break; case 40: if(hash.matches("[a-fA-F0-9]{40}")) { return new Checksum(HashAlgorithm.sha1, hash); } break; case 64: if(hash.matches("[A-Fa-f0-9]{64}")) { return new Checksum(HashAlgorithm.sha256, hash); } break; case 128: if(hash.matches("[A-Fa-f0-9]{128}")) { return new Checksum(HashAlgorithm.sha512, hash); } break; default: log.warn(String.format("Failure to detect algorithm for checksum %s", hash)); } return Checksum.NONE; } Checksum(final HashAlgorithm algorithm, final String hash); @Override String toString(); static Checksum parse(final String hash); @Override boolean equals(final Object o); @Override int hashCode(); }
Checksum { public static Checksum parse(final String hash) { if(StringUtils.isBlank(hash)) { return Checksum.NONE; } switch(hash.length()) { case 8: if(hash.matches("[a-fA-F0-9]{8}")) { return new Checksum(HashAlgorithm.crc32, hash); } break; case 32: if(hash.matches("[a-fA-F0-9]{32}")) { return new Checksum(HashAlgorithm.md5, hash); } break; case 40: if(hash.matches("[a-fA-F0-9]{40}")) { return new Checksum(HashAlgorithm.sha1, hash); } break; case 64: if(hash.matches("[A-Fa-f0-9]{64}")) { return new Checksum(HashAlgorithm.sha256, hash); } break; case 128: if(hash.matches("[A-Fa-f0-9]{128}")) { return new Checksum(HashAlgorithm.sha512, hash); } break; default: log.warn(String.format("Failure to detect algorithm for checksum %s", hash)); } return Checksum.NONE; } Checksum(final HashAlgorithm algorithm, final String hash); @Override String toString(); static Checksum parse(final String hash); @Override boolean equals(final Object o); @Override int hashCode(); final HashAlgorithm algorithm; final String hash; static final Checksum NONE; }