summaryrefslogtreecommitdiff
path: root/src/main/java/com/keuin/kbackupfabric/backup/incremental/identifier/Sha256Identifier.java
blob: 21111a0a37ca069859b9a769680ca6a7c0ac64ba (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
package com.keuin.kbackupfabric.backup.incremental.identifier;

import com.keuin.kbackupfabric.util.BytesUtil;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Objects;

/**
 * Identifier based on sha256.
 * Immutable.
 */
public class Sha256Identifier extends SingleHashIdentifier {

    private static final int SHA256_LENGTH = 32;
    private static final Sha256Identifier DUMMY = new Sha256Identifier(new byte[SHA256_LENGTH]); // only for using its hash method
    private static final FileIdentifierProvider<Sha256Identifier> factory = Sha256Identifier::fromFile;
    private static final String marker = "S2";

    public static Sha256Identifier fromFile(File file) throws IOException {
        if (!file.isFile()) {
            throw new IllegalArgumentException("file is not a file");
        }
        return new Sha256Identifier(DUMMY.hash(file));
    }

    /**
     * Load sha-256 from a named file. Only used in StorageObjectLoader.
     *
     * @param fileName the file name.
     * @return identifier.
     */
    static Sha256Identifier fromFileName(String fileName) {
        if (!fileName.matches(marker + "-[0-9A-Fa-f]{32}"))
            return null;
        String hexString = fileName.substring(marker.length() + 1);
        return new Sha256Identifier(BytesUtil.hexToBytes(hexString));
    }

    public static FileIdentifierProvider<Sha256Identifier> getFactory() {
        return factory;
    }

    protected Sha256Identifier(byte[] hash) {
        super(hash, marker);
        Objects.requireNonNull(hash);
        if (hash.length != SHA256_LENGTH) {
            throw new IllegalStateException(String.format("SHA256 must be %d bytes", SHA256_LENGTH));
        }
    }

    @Override
    protected byte[] hash(File file) throws IOException {
        return sha256Hash(file);
    }

    public static byte[] sha256Hash(File file) throws IOException {
        try {
            MessageDigest digest = MessageDigest.getInstance("SHA-256");

            try (FileInputStream inputStream = new FileInputStream(file)) {
                byte[] readBuffer = new byte[1024 * 1024];

                int readLength;
                while ((readLength = inputStream.read(readBuffer)) > 0) {
                    digest.update(readBuffer, 0, readLength);
                }

                return digest.digest();
            }

        } catch (NoSuchAlgorithmException ignored) {
            // this shouldn't happen
            return new byte[SHA256_LENGTH];
        }
    }

}