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 if (type == null)
74 throw new IllegalArgumentException("Checksum type is null");
75 this.value = value;
76 this.type = type;
77 }
78
79
80
81
82
83
84 public ChecksumType getType() {
85 return type;
86 }
87
88
89
90
91
92
93 public String getValue() {
94 return value;
95 }
96
97
98
99
100
101
102
103
104 public static String convertToHex(byte[] data) {
105 final StringBuffer buf = new StringBuffer();
106 for (int i = 0; i < data.length; i++) {
107 int halfbyte = (data[i] >>> 4) & 0x0F;
108 int twoHalfs = 0;
109 do {
110 if ((0 <= halfbyte) && (halfbyte <= 9))
111 buf.append((char) ('0' + halfbyte));
112 else
113 buf.append((char) ('a' + (halfbyte - 10)));
114 halfbyte = data[i] & 0x0F;
115 } while (twoHalfs++ < 1);
116 }
117 return buf.toString();
118 }
119
120 @Override
121 public boolean equals(Object obj) {
122 if (obj instanceof Checksum) {
123 Checksum c = (Checksum) obj;
124 return type.equals(c.type) && value.equals(c.value);
125 }
126 return false;
127 }
128
129 @Override
130 public int hashCode() {
131 return value.hashCode();
132 }
133
134 @Override
135 public String toString() {
136 return value + " (" + type + ")";
137 }
138
139
140
141
142
143
144
145
146
147
148 public static Checksum fromString(String checksum) throws NoSuchAlgorithmException {
149 String[] checksumParts = checksum.split(" ");
150
151 if (checksumParts.length != 2) {
152 throw new IllegalArgumentException("Invalid string for checksum!");
153 }
154
155 String value = checksumParts[0];
156 String type = checksumParts[1].replace("(","").replace(")", "");
157
158 return create(type, value);
159 }
160
161
162
163
164
165
166
167
168
169
170
171
172 public static Checksum create(String type, String value) throws NoSuchAlgorithmException {
173 ChecksumType t = ChecksumType.fromString(type);
174 return new Checksum(value, t);
175 }
176
177
178
179
180
181
182
183
184
185
186 public static Checksum create(ChecksumType type, String value) {
187 return new Checksum(value, type);
188 }
189
190
191
192
193
194
195
196
197
198
199
200
201 public static Checksum create(ChecksumType type, File file) throws IOException {
202 return create(type, new BufferedInputStream(new FileInputStream(file)));
203 }
204
205
206
207
208
209 public static Checksum create(ChecksumType type, InputStream is) throws IOException {
210 MessageDigest checksum;
211 try {
212 checksum = MessageDigest.getInstance(type.getName());
213 } catch (NoSuchAlgorithmException e) {
214 throw new IllegalStateException("This system does not support checksums of type " + type.getName());
215 }
216 try {
217 byte[] bytes = new byte[1024];
218 int len = 0;
219 while ((len = is.read(bytes)) >= 0) {
220 checksum.update(bytes, 0, len);
221 }
222 } finally {
223 IoSupport.closeQuietly(is);
224 }
225 return new Checksum(convertToHex(checksum.digest()), type);
226 }
227
228 }