001package net.filebot.hash;
002
003import java.io.File;
004import java.io.IOException;
005import java.util.zip.CRC32;
006
007import net.filebot.CategoryFileFilter;
008import net.filebot.MediaTypes;
009
010public enum HashType {
011
012        SFV {
013
014                @Override
015                public Hash newHash() {
016                        return new ChecksumHash(new CRC32());
017                }
018
019                @Override
020                public String getAlgorithm() {
021                        return "CRC32";
022                }
023
024                @Override
025                public VerificationFormat getFormat() {
026                        // e.g folder/file.txt 970E4EF1
027                        return new SfvFormat();
028                }
029
030                @Override
031                public CategoryFileFilter getFilter() {
032                        return new CategoryFileFilter("SFV", MediaTypes.SFV);
033                }
034
035                @Override
036                public String toString() {
037                        return "SFV";
038                }
039        },
040
041        MD5 {
042
043                @Override
044                public Hash newHash() {
045                        return new MessageDigestHash(getAlgorithm());
046                }
047
048                @Override
049                public String getAlgorithm() {
050                        return "MD5";
051                }
052
053                @Override
054                public VerificationFormat getFormat() {
055                        // e.g. 50e85fe18e17e3616774637a82968f4c *folder/file.txt
056                        return new VerificationFormat();
057                }
058
059                @Override
060                public CategoryFileFilter getFilter() {
061                        return new CategoryFileFilter("md5sum", MediaTypes.MD5);
062                }
063
064                @Override
065                public String toString() {
066                        return "MD5";
067                }
068        },
069
070        SHA1 {
071
072                @Override
073                public Hash newHash() {
074                        return new MessageDigestHash(getAlgorithm());
075                }
076
077                @Override
078                public String getAlgorithm() {
079                        return "SHA-1";
080                }
081
082                @Override
083                public VerificationFormat getFormat() {
084                        // e.g 1a02a7c1e9ac91346d08829d5037b240f42ded07 ?SHA1*folder/file.txt
085                        return new VerificationFormat("SHA1");
086                }
087
088                @Override
089                public CategoryFileFilter getFilter() {
090                        return new CategoryFileFilter("sha1sum", MediaTypes.SHA1);
091                }
092
093                @Override
094                public String toString() {
095                        return "SHA1";
096                }
097        },
098
099        SHA256 {
100
101                @Override
102                public Hash newHash() {
103                        return new MessageDigestHash(getAlgorithm());
104                }
105
106                @Override
107                public String getAlgorithm() {
108                        return "SHA-256";
109                }
110
111                @Override
112                public VerificationFormat getFormat() {
113                        // e.g 1a02a7c1e9ac91346d08829d5037b240f42ded07 ?SHA256*folder/file.txt
114                        return new VerificationFormat("SHA256");
115                }
116
117                @Override
118                public CategoryFileFilter getFilter() {
119                        return new CategoryFileFilter("sha256sum", MediaTypes.SHA256);
120                }
121
122                @Override
123                public String toString() {
124                        return "SHA2";
125                }
126        },
127
128        SHA3_384 {
129
130                @Override
131                public Hash newHash() {
132                        return new MessageDigestHash(getAlgorithm());
133                }
134
135                @Override
136                public String getAlgorithm() {
137                        return "SHA3-384";
138                }
139
140                @Override
141                public VerificationFormat getFormat() {
142                        // e.g 1a02a7c1e9ac91346d08829d5037b240f42ded07 ?SHA3-384*folder/file.txt
143                        return new VerificationFormat("SHA3-384");
144                }
145
146                @Override
147                public CategoryFileFilter getFilter() {
148                        return new CategoryFileFilter("sha3sum", MediaTypes.SHA3);
149                }
150
151                @Override
152                public String toString() {
153                        return "SHA3";
154                }
155        };
156
157        public abstract Hash newHash();
158
159        public abstract String getAlgorithm();
160
161        public abstract VerificationFormat getFormat();
162
163        public abstract CategoryFileFilter getFilter();
164
165        public VerificationFileReader newReader(File file) throws IOException {
166                return VerificationFileReader.open(file, getFormat());
167        };
168
169}