Java by API/java.security/Security
Security: addProvider(Provider provider)
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.security.DigestInputStream;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.Provider;
import java.security.Security;
import java.util.zip.CRC32;
public class MainClass {
static private String hexDigit(byte x) {
StringBuffer sb = new StringBuffer();
char c;
// First nibble
c = (char) ((x >> 4) & 0xf);
if (c > 9) {
c = (char) ((c - 10) + "a");
} else {
c = (char) (c + "0");
}
sb.append(c);
// Second nibble
c = (char) (x & 0xf);
if (c > 9) {
c = (char) ((c - 10) + "a");
} else {
c = (char) (c + "0");
}
sb.append(c);
return sb.toString();
}
static private String computeDigest(MessageDigest algorithm, String filename) {
String returnValue = "";
try {
algorithm.reset();
FileInputStream fis = new FileInputStream(filename);
BufferedInputStream bis = new BufferedInputStream(fis);
DigestInputStream dis = new DigestInputStream(bis, algorithm);
int ch;
while ((ch = dis.read()) != -1)
;
StringBuffer hexString = new StringBuffer();
byte digest[] = algorithm.digest();
int digestLength = digest.length;
for (int i = 0; i < digestLength; i++) {
hexString.append(hexDigit(digest[i]));
hexString.append(" ");
}
returnValue = hexString.toString();
} catch (IOException e) {
System.err.println("Error generating digest for: " + filename);
}
return returnValue;
}
public static void main(String args[]) {
MessageDigest algorithm = null;
Security.addProvider(new MyProvider());
try {
algorithm = MessageDigest.getInstance("CRC32");
} catch (NoSuchAlgorithmException e) {
System.err.println("Invalid algorithm");
System.exit(-1);
}
int argsLength = args.length;
for (int i = 0; i < argsLength; i++) {
String digest = computeDigest(algorithm, args[i]);
System.out.println(args[i] + " : " + digest);
}
}
}
class MyProvider extends Provider {
public MyProvider() {
super("MyProvider", 1.0, "jexp.ru Collections Example");
put("CRC32", "CRC");
}
}
class CRC extends MessageDigest {
CRC32 crc;
public CRC() {
super("CRC");
crc = new CRC32();
}
protected void engineReset() {
crc.reset();
}
protected void engineUpdate(byte input) {
crc.update(input);
}
protected void engineUpdate(byte[] input, int offset, int len) {
crc.update(input, offset, len);
}
protected byte[] engineDigest() {
long l = crc.getValue();
byte[] bytes = new byte[4];
bytes[3] = (byte) ((l & 0xFF000000) >> 24);
bytes[2] = (byte) ((l & 0x00FF0000) >> 16);
bytes[1] = (byte) ((l & 0x0000FF00) >> 8);
bytes[0] = (byte) ((l & 0x000000FF) >> 0);
return bytes;
}
}
Security: getProviders()
/*
Ciphers:
None available.
KeyAgreeents:
None available.
Macs:
None available.
MessageDigests:
SHA-512
SHA1
MD2
SHA
SHA ImplementedIn
SHA-256
MD5 ImplementedIn
SHA-1
MD5
SHA-384
Signatures:
OID.1.2.840.10040.4.3
OID.1.2.840.113549.1.1.4
SHA384withRSA
1.3.14.3.2.29
SHA512withRSA SupportedKeyClasses
SHA/DSA
SHA1withDSA KeySize
NONEwithDSA SupportedKeyClasses
OID.1.2.840.113549.1.1.5
SHA512withRSA
MD5withRSA
DSS
OID.1.2.840.113549.1.1.11
SHA384withRSA SupportedKeyClasses
SHA1withRSA
MD5withRSA SupportedKeyClasses
NONEwithDSA
1.2.840.113549.1.1.4
MD5andSHA1withRSA
1.2.840.113549.1.1.11
OID.1.2.840.113549.1.1.13
1.3.14.3.2.27
1.2.840.10040.4.3
SHA256withRSA
MD2withRSA SupportedKeyClasses
1.2.840.113549.1.1.2
1.2.840.113549.1.1.12
RawDSA
SHA1withDSA
SHA1/DSA
MD2withRSA
1.3.14.3.2.13
SHAwithDSA
DSAWithSHA1
1.2.840.113549.1.1.13
OID.1.3.14.3.2.29
SHA1withDSA ImplementedIn
SHA256withRSA SupportedKeyClasses
SHA1withDSA SupportedKeyClasses
DSA
1.2.840.113549.1.1.5
SHA-1/DSA
SHA1withRSA SupportedKeyClasses
OID.1.2.840.113549.1.1.12
OID.1.2.840.113549.1.1.2
*/
import java.security.Provider;
import java.security.Security;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
public class ListAlgorithms {
public static void printSet(String setName, Set algorithms) {
System.out.println(setName + ":");
if (algorithms.isEmpty()) {
System.out.println(" None available.");
} else {
Iterator it = algorithms.iterator();
while (it.hasNext()) {
String name = (String) it.next();
System.out.println(" " + name);
}
}
}
public static void main(String[] args) {
Provider[] providers = Security.getProviders();
Set<String> ciphers = new HashSet<String>();
Set<String> keyAgreements = new HashSet<String>();
Set<String> macs = new HashSet<String>();
Set<String> messageDigests = new HashSet<String>();
Set<String> signatures = new HashSet<String>();
for (int i = 0; i != providers.length; i++) {
Iterator it = providers[i].keySet().iterator();
while (it.hasNext()) {
String entry = (String) it.next();
if (entry.startsWith("Alg.Alias.")) {
entry = entry.substring("Alg.Alias.".length());
}
if (entry.startsWith("Cipher.")) {
ciphers.add(entry.substring("Cipher.".length()));
} else if (entry.startsWith("KeyAgreement.")) {
keyAgreements.add(entry.substring("KeyAgreement.".length()));
} else if (entry.startsWith("Mac.")) {
macs.add(entry.substring("Mac.".length()));
} else if (entry.startsWith("MessageDigest.")) {
messageDigests.add(entry.substring("MessageDigest.".length()));
} else if (entry.startsWith("Signature.")) {
signatures.add(entry.substring("Signature.".length()));
}
}
}
printSet("Ciphers", ciphers);
printSet("KeyAgreeents", keyAgreements);
printSet("Macs", macs);
printSet("MessageDigests", messageDigests);
printSet("Signatures", signatures);
}
}