1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 package org.opencastproject.util;
24
25 import java.io.BufferedInputStream;
26 import java.io.File;
27 import java.io.FileInputStream;
28 import java.io.IOException;
29 import java.io.InputStream;
30 import java.io.Serializable;
31 import java.security.MessageDigest;
32 import java.security.NoSuchAlgorithmException;
33
34 import javax.xml.bind.annotation.XmlAccessType;
35 import javax.xml.bind.annotation.XmlAccessorType;
36 import javax.xml.bind.annotation.XmlAttribute;
37 import javax.xml.bind.annotation.XmlType;
38 import javax.xml.bind.annotation.XmlValue;
39
40
41
42
43 @XmlAccessorType(XmlAccessType.NONE)
44 @XmlType(name = "checksum", namespace = "http://mediapackage.opencastproject.org")
45 public final class Checksum implements Serializable {
46
47
48 private static final long serialVersionUID = 1L;
49
50
51 @XmlValue
52 protected String value = null;
53
54
55 @XmlAttribute(name = "type")
56 protected ChecksumType type = null;
57
58
59 public Checksum() {
60 }
61
62
63
64
65
66
67
68
69
70 private Checksum(String value, ChecksumType type) {
71 if (value == null) {
72 throw new IllegalArgumentException("Checksum value is null");
73 }
74 if (type == null) {
75 throw new IllegalArgumentException("Checksum type is null");
76 }
77 this.value = value;
78 this.type = type;
79 }
80
81
82
83
84
85
86 public ChecksumType getType() {
87 return type;
88 }
89
90
91
92
93
94
95 public String getValue() {
96 return value;
97 }
98
99
100
101
102
103
104
105
106 public static String convertToHex(byte[] data) {
107 final StringBuffer buf = new StringBuffer();
108 for (int i = 0; i < data.length; i++) {
109 int halfbyte = (data[i] >>> 4) & 0x0F;
110 int twoHalfs = 0;
111 do {
112 if ((0 <= halfbyte) && (halfbyte <= 9)) {
113 buf.append((char) ('0' + halfbyte));
114 } else {
115 buf.append((char) ('a' + (halfbyte - 10)));
116 }
117 halfbyte = data[i] & 0x0F;
118 } while (twoHalfs++ < 1);
119 }
120 return buf.toString();
121 }
122
123 @Override
124 public boolean equals(Object obj) {
125 if (obj instanceof Checksum) {
126 Checksum c = (Checksum) obj;
127 return type.equals(c.type) && value.equals(c.value);
128 }
129 return false;
130 }
131
132 @Override
133 public int hashCode() {
134 return value.hashCode();
135 }
136
137 @Override
138 public String toString() {
139 return value + " (" + type + ")";
140 }
141
142
143
144
145
146
147
148
149
150
151 public static Checksum fromString(String checksum) throws NoSuchAlgorithmException {
152 String[] checksumParts = checksum.split(" ");
153
154 if (checksumParts.length != 2) {
155 throw new IllegalArgumentException("Invalid string for checksum!");
156 }
157
158 String value = checksumParts[0];
159 String type = checksumParts[1].replace("(","").replace(")", "");
160
161 return create(type, value);
162 }
163
164
165
166
167
168
169
170
171
172
173
174
175 public static Checksum create(String type, String value) throws NoSuchAlgorithmException {
176 ChecksumType t = ChecksumType.fromString(type);
177 return new Checksum(value, t);
178 }
179
180
181
182
183
184
185
186
187
188
189 public static Checksum create(ChecksumType type, String value) {
190 return new Checksum(value, type);
191 }
192
193
194
195
196
197
198
199
200
201
202
203
204 public static Checksum create(ChecksumType type, File file) throws IOException {
205 return create(type, new BufferedInputStream(new FileInputStream(file)));
206 }
207
208
209
210
211
212 public static Checksum create(ChecksumType type, InputStream is) throws IOException {
213 MessageDigest checksum;
214 try {
215 checksum = MessageDigest.getInstance(type.getName());
216 } catch (NoSuchAlgorithmException e) {
217 throw new IllegalStateException("This system does not support checksums of type " + type.getName());
218 }
219 try {
220 byte[] bytes = new byte[1024];
221 int len = 0;
222 while ((len = is.read(bytes)) >= 0) {
223 checksum.update(bytes, 0, len);
224 }
225 } finally {
226 IoSupport.closeQuietly(is);
227 }
228 return new Checksum(convertToHex(checksum.digest()), type);
229 }
230
231 }