1
0
mirror of https://github.com/mitb-archive/filebot synced 2024-08-13 17:03:45 -04:00

Refactor Hash

This commit is contained in:
Reinhard Pointner 2017-05-26 17:13:51 +08:00
parent eaa038c66e
commit eabd0125c0
4 changed files with 37 additions and 47 deletions

View File

@ -1,36 +0,0 @@
package net.filebot.hash;
import java.math.BigInteger;
import java.security.NoSuchAlgorithmException;
import jonelo.jacksum.algorithm.Edonkey;
public class Ed2kHash implements Hash {
private final Edonkey ed2k;
public Ed2kHash() {
try {
this.ed2k = new Edonkey();
} catch (NoSuchAlgorithmException e) {
throw new RuntimeException(e);
}
}
@Override
public void update(byte[] bytes, int off, int len) {
ed2k.update(bytes, off, len);
}
@Override
public String digest() {
return String.format("%0" + (ed2k.getByteArray().length * 2) + "x", new BigInteger(1, ed2k.getByteArray()));
}
}

View File

@ -78,7 +78,7 @@ public enum HashType {
@Override
public VerificationFormat getFormat() {
// e.g 1a02a7c1e9ac91346d08829d5037b240f42ded07 ?SHA1*folder/file.txt
// e.g 1a02a7c1e9ac91346d08829d5037b240f42ded07 ?SHA256*folder/file.txt
return new VerificationFormat("SHA256");
}
@ -97,7 +97,7 @@ public enum HashType {
@Override
public Hash newHash() {
return new Ed2kHash();
return JacksumHash.newED2K();
}
@Override

View File

@ -0,0 +1,33 @@
package net.filebot.hash;
import jonelo.jacksum.algorithm.AbstractChecksum;
import jonelo.jacksum.algorithm.Edonkey;
public class JacksumHash implements Hash {
private final AbstractChecksum checksum;
public JacksumHash(AbstractChecksum checksum) {
this.checksum = checksum;
}
@Override
public void update(byte[] bytes, int off, int len) {
checksum.update(bytes, off, len);
}
@Override
public String digest() {
return checksum.getFormattedValue();
}
public static Hash newED2K() {
try {
return new JacksumHash(new Edonkey());
} catch (Exception e) {
throw new IllegalStateException(e);
}
}
}

View File

@ -1,37 +1,30 @@
package net.filebot.hash;
import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
public class MessageDigestHash implements Hash {
private final MessageDigest md;
public MessageDigestHash(String algorithm) {
try {
this.md = MessageDigest.getInstance(algorithm);
} catch (NoSuchAlgorithmException e) {
throw new IllegalArgumentException(e);
} catch (Exception e) {
throw new IllegalStateException(e);
}
}
public MessageDigestHash(MessageDigest md) {
this.md = md;
}
@Override
public void update(byte[] bytes, int off, int len) {
md.update(bytes, off, len);
}
@Override
public String digest() {
// e.g. %032x (format for MD-5)