Skip to content

Commit 82d967a

Browse files
committed
[CODEC-212] Create a minimal Digest command line utility: org.apache.commons.codec.digest.Digest.
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/codec/trunk@1743780 13f79535-47bb-0310-9956-ffa450edef68
1 parent a82070e commit 82d967a

2 files changed

Lines changed: 121 additions & 0 deletions

File tree

src/changes/changes.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ The <action> type attribute can be add,update,fix,remove.
4747
<action dev="ggregory" type="fix" issue="CODEC-207" due-to="Gary Gregory">Charsets Javadoc breaks build when using Java 8</action>
4848
<action dev="ggregory" type="fix" issue="CODEC-199" due-to="Yossi Tamari">Bug in HW rule in Soundex</action>
4949
<action dev="ggregory" type="fix" issue="CODEC-209" due-to="Gary Gregory">Javadoc for SHA-224 DigestUtils methods should mention Java 1.8.0 restriction instead of 1.4.0.</action>
50+
<action dev="ggregory" type="add" issue="CODEC-212" due-to="Gary Gregory">Create a minimal Digest command line utility: org.apache.commons.codec.digest.Digest</action>
5051
<action dev="ggregory" type="add" issue="CODEC-211" due-to="Gary Gregory">Create enum MessageDigestAlgorithm and deprecate class MessageDigestAlgorithms</action>
5152
<action dev="ggregory" type="add" issue="CODEC-210" due-to="Gary Gregory">Add DigestUtils.getDigest(String, MessageDigest)</action>
5253
<action dev="ggregory" type="add" issue="CODEC-208" due-to="Gary Gregory">Make some DigestUtils APIs public</action>
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package org.apache.commons.codec.digest;
18+
19+
import java.io.File;
20+
import java.io.IOException;
21+
import java.security.MessageDigest;
22+
23+
import org.apache.commons.codec.binary.Hex;
24+
25+
/**
26+
* A minimal command line to run digest over files.
27+
*
28+
* @see #main(String[])
29+
*/
30+
public class Digest {
31+
32+
/**
33+
* Runs the digest algorithm in {@code args[0]} on the file in {@code args[1]}. If there is no {@code args[1]}, use
34+
* standard input.
35+
*
36+
* <p>
37+
* The algorithm can also be {@code ALL} or {@code *} to output one line for each known algorithm.
38+
* </p>
39+
*
40+
* @param args
41+
* {@code args[0]} is one of {@link MessageDigestAlgorithm} name, {@link MessageDigest} name, {@code ALL}
42+
* , or {@code *}. {@code args[1]} is a FILE.
43+
* @throws IOException
44+
*/
45+
public static void main(String[] args) throws IOException {
46+
new Digest(args).run();
47+
}
48+
49+
private final String algorithm;
50+
private final String[] args;
51+
private final String source;
52+
53+
private Digest(final String[] args) {
54+
if (args == null) {
55+
throw new IllegalArgumentException("args");
56+
}
57+
if (args.length == 0) {
58+
throw new IllegalArgumentException(
59+
String.format("Usage: java %s [algorithm] [FILE|DIRECTORY]", Digest.class.getName()));
60+
}
61+
this.args = args;
62+
algorithm = args[0];
63+
source = args.length == 1 ? null : args[1];
64+
}
65+
66+
private void println(String prefix, final byte[] digest) {
67+
final String sourceDesc = source == null ? "-" : source;
68+
System.out.println(prefix + Hex.encodeHexString(digest) + " " + sourceDesc);
69+
}
70+
71+
private void run() throws IOException {
72+
if (algorithm.equalsIgnoreCase("ALL") || algorithm.equals("*")) {
73+
run(MessageDigestAlgorithm.values());
74+
return;
75+
}
76+
final MessageDigest messageDigest = DigestUtils.getDigest(algorithm, null);
77+
if (messageDigest != null) {
78+
run("", messageDigest);
79+
} else {
80+
run("", MessageDigestAlgorithm.valueOf(algorithm).getMessageDigest());
81+
}
82+
}
83+
84+
private void run(MessageDigestAlgorithm[] digestAlgorithms) throws IOException {
85+
for (int i = 0; i < digestAlgorithms.length; i++) {
86+
final MessageDigestAlgorithm messageDigestAlgorithm = digestAlgorithms[i];
87+
if (messageDigestAlgorithm.isAvailable()) {
88+
run(messageDigestAlgorithm.getAlgorithm() + " ", messageDigestAlgorithm);
89+
}
90+
}
91+
}
92+
93+
private void run(String prefix, final MessageDigest messageDigest) throws IOException {
94+
if (source == null) {
95+
println(prefix, DigestUtils.digest(messageDigest, System.in));
96+
return;
97+
}
98+
final File file = new File(source);
99+
if (file.isFile()) {
100+
println(prefix, DigestUtils.digest(messageDigest, file));
101+
} else if (file.isDirectory()) {
102+
run(prefix, messageDigest, file.listFiles());
103+
}
104+
}
105+
106+
private void run(String prefix, MessageDigest messageDigest, File[] files) throws IOException {
107+
for (int i = 0; i < files.length; i++) {
108+
println(prefix, DigestUtils.digest(messageDigest, files[i]));
109+
}
110+
}
111+
112+
private void run(String prefix, final MessageDigestAlgorithm messageDigestAlgorithm) throws IOException {
113+
run(prefix, messageDigestAlgorithm.getMessageDigest());
114+
}
115+
116+
@Override
117+
public String toString() {
118+
return String.format("%s[%s]", super.toString(), args);
119+
}
120+
}

0 commit comments

Comments
 (0)