Latest bnd code
git-svn-id: https://svn.apache.org/repos/asf/felix/trunk@1350613 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/bundleplugin/src/main/java/aQute/libg/cryptography/Crypto.java b/bundleplugin/src/main/java/aQute/libg/cryptography/Crypto.java
index ff971e8..22eac45 100644
--- a/bundleplugin/src/main/java/aQute/libg/cryptography/Crypto.java
+++ b/bundleplugin/src/main/java/aQute/libg/cryptography/Crypto.java
@@ -7,61 +7,57 @@
import java.util.regex.*;
public class Crypto {
- static final Pattern RSA_PRIVATE = Pattern
- .compile("\\s*RSA\\.Private\\((\\p{XDigit})+:(\\p{XDigit})+\\)\\s*");
- static final Pattern RSA_PUBLIC = Pattern
- .compile("\\s*RSA\\.Public\\((\\p{XDigit})+:(\\p{XDigit})+\\)\\s*");
+ static final Pattern RSA_PRIVATE = Pattern.compile("\\s*RSA\\.Private\\((\\p{XDigit})+:(\\p{XDigit})+\\)\\s*");
+ static final Pattern RSA_PUBLIC = Pattern.compile("\\s*RSA\\.Public\\((\\p{XDigit})+:(\\p{XDigit})+\\)\\s*");
/**
- *
* @param <T>
* @param spec
* @return
* @throws Exception
*/
- @SuppressWarnings("unchecked") public static <T> T fromString(String spec, Class<T> c) throws Exception {
- if ( PrivateKey.class.isAssignableFrom(c)) {
- Matcher m = RSA_PRIVATE.matcher(spec);
- if ( m.matches()) {
- return (T) RSA.createPrivate(
- new BigInteger(m.group(1)), new BigInteger(m.group(2)));
+ @SuppressWarnings("unchecked")
+ public static <T> T fromString(String spec, Class<T> c) throws Exception {
+ if (PrivateKey.class.isAssignableFrom(c)) {
+ Matcher m = RSA_PRIVATE.matcher(spec);
+ if (m.matches()) {
+ return (T) RSA.createPrivate(new BigInteger(m.group(1)), new BigInteger(m.group(2)));
}
- throw new IllegalArgumentException("No such private key " + spec );
+ throw new IllegalArgumentException("No such private key " + spec);
}
-
- if ( PublicKey.class.isAssignableFrom(c)) {
- Matcher m = RSA_PUBLIC.matcher(spec);
- if ( m.matches()) {
- return (T) RSA.create( new RSAPublicKeySpec(
- new BigInteger(m.group(1)), new BigInteger(m.group(2))));
+
+ if (PublicKey.class.isAssignableFrom(c)) {
+ Matcher m = RSA_PUBLIC.matcher(spec);
+ if (m.matches()) {
+ return (T) RSA.create(new RSAPublicKeySpec(new BigInteger(m.group(1)), new BigInteger(m.group(2))));
}
- throw new IllegalArgumentException("No such public key " + spec );
+ throw new IllegalArgumentException("No such public key " + spec);
}
return null;
}
- public static String toString( Object key ) {
- if ( key instanceof RSAPrivateKey ) {
+ public static String toString(Object key) {
+ if (key instanceof RSAPrivateKey) {
RSAPrivateKey pk = (RSAPrivateKey) key;
return "RSA.Private(" + pk.getModulus() + ":" + pk.getPrivateExponent() + ")";
}
- if ( key instanceof RSAPublicKey ) {
+ if (key instanceof RSAPublicKey) {
RSAPublicKey pk = (RSAPublicKey) key;
return "RSA.Private(" + pk.getModulus() + ":" + pk.getPublicExponent() + ")";
}
return null;
}
-
-// public static <T extends Digest> Signer<T> signer(PrivateKey key, Digester<T> digester) throws NoSuchAlgorithmException {
-// Signature s = Signature.getInstance(key.getAlgorithm() + "with" + digester.getAlgorithm());
-// return new Signer<T>(s,digester);
-// }
+ // public static <T extends Digest> Signer<T> signer(PrivateKey key,
+ // Digester<T> digester) throws NoSuchAlgorithmException {
+ // Signature s = Signature.getInstance(key.getAlgorithm() + "with" +
+ // digester.getAlgorithm());
+ // return new Signer<T>(s,digester);
+ // }
public static Verifier verifier(PublicKey key, Digest digest) throws NoSuchAlgorithmException {
Signature s = Signature.getInstance(key.getAlgorithm() + "with" + digest.getAlgorithm());
- return new Verifier(s,digest);
+ return new Verifier(s, digest);
}
-
}
diff --git a/bundleplugin/src/main/java/aQute/libg/cryptography/Digest.java b/bundleplugin/src/main/java/aQute/libg/cryptography/Digest.java
index 90e7b1a..93d48ad 100644
--- a/bundleplugin/src/main/java/aQute/libg/cryptography/Digest.java
+++ b/bundleplugin/src/main/java/aQute/libg/cryptography/Digest.java
@@ -10,30 +10,28 @@
protected Digest(byte[] checksum, int width) {
this.digest = checksum;
if (digest.length != width)
- throw new IllegalArgumentException("Invalid width for digest: " + digest.length
- + " expected " + width);
+ throw new IllegalArgumentException("Invalid width for digest: " + digest.length + " expected " + width);
}
-
public byte[] digest() {
return digest;
}
- @Override public String toString() {
+ @Override
+ public String toString() {
return String.format("%s(d=%s)", getAlgorithm(), Hex.toHexString(digest));
}
public abstract String getAlgorithm();
-
-
+
public boolean equals(Object other) {
- if ( !(other instanceof Digest))
+ if (!(other instanceof Digest))
return false;
Digest d = (Digest) other;
return Arrays.equals(d.digest, digest);
}
-
+
public int hashCode() {
return Arrays.hashCode(digest);
}
diff --git a/bundleplugin/src/main/java/aQute/libg/cryptography/Digester.java b/bundleplugin/src/main/java/aQute/libg/cryptography/Digester.java
index eeb2c8f..2785c3d 100644
--- a/bundleplugin/src/main/java/aQute/libg/cryptography/Digester.java
+++ b/bundleplugin/src/main/java/aQute/libg/cryptography/Digester.java
@@ -7,41 +7,45 @@
public abstract class Digester<T extends Digest> extends OutputStream {
protected MessageDigest md;
- OutputStream out[];
-
- public Digester(MessageDigest instance, OutputStream ... out) {
+ OutputStream out[];
+
+ public Digester(MessageDigest instance, OutputStream... out) {
md = instance;
this.out = out;
}
-
+
@Override
- public void write( byte[] buffer, int offset, int length) throws IOException{
- md.update(buffer,offset,length);
- for ( OutputStream o : out ) {
+ public void write(byte[] buffer, int offset, int length) throws IOException {
+ md.update(buffer, offset, length);
+ for (OutputStream o : out) {
o.write(buffer, offset, length);
}
}
+
@Override
- public void write( int b) throws IOException{
+ public void write(int b) throws IOException {
md.update((byte) b);
- for ( OutputStream o : out ) {
+ for (OutputStream o : out) {
o.write(b);
}
}
-
+
public MessageDigest getMessageDigest() throws Exception {
return md;
}
-
+
public T from(InputStream in) throws Exception {
- IO.copy(in,this);
+ IO.copy(in, this);
return digest();
}
-
- public void setOutputs(OutputStream ...out) {
+
+ public void setOutputs(OutputStream... out) {
this.out = out;
}
+
public abstract T digest() throws Exception;
- public abstract T digest( byte [] bytes) throws Exception;
+
+ public abstract T digest(byte[] bytes) throws Exception;
+
public abstract String getAlgorithm();
}
diff --git a/bundleplugin/src/main/java/aQute/libg/cryptography/Key.java b/bundleplugin/src/main/java/aQute/libg/cryptography/Key.java
index 160ec05..f4fc217 100644
--- a/bundleplugin/src/main/java/aQute/libg/cryptography/Key.java
+++ b/bundleplugin/src/main/java/aQute/libg/cryptography/Key.java
@@ -1,8 +1,6 @@
package aQute.libg.cryptography;
-
public abstract class Key implements java.security.Key {
private static final long serialVersionUID = 1L;
-
}
diff --git a/bundleplugin/src/main/java/aQute/libg/cryptography/MD5.java b/bundleplugin/src/main/java/aQute/libg/cryptography/MD5.java
index 84b53ee..05a6bdd 100644
--- a/bundleplugin/src/main/java/aQute/libg/cryptography/MD5.java
+++ b/bundleplugin/src/main/java/aQute/libg/cryptography/MD5.java
@@ -3,32 +3,36 @@
import java.io.*;
import java.security.*;
-
-
public class MD5 extends Digest {
- public final static String ALGORITHM = "MD5";
-
- public static Digester<MD5> getDigester(OutputStream ... out) throws Exception {
+ public final static String ALGORITHM = "MD5";
+
+ public static Digester<MD5> getDigester(OutputStream... out) throws Exception {
return new Digester<MD5>(MessageDigest.getInstance(ALGORITHM), out) {
-
- @Override public MD5 digest() throws Exception {
+
+ @Override
+ public MD5 digest() throws Exception {
return new MD5(md.digest());
}
- @Override public MD5 digest(byte[] bytes) {
+ @Override
+ public MD5 digest(byte[] bytes) {
return new MD5(bytes);
}
- @Override public String getAlgorithm() {
+
+ @Override
+ public String getAlgorithm() {
return ALGORITHM;
}
};
}
-
-
+
public MD5(byte[] digest) {
- super(digest,16);
+ super(digest, 16);
}
- @Override public String getAlgorithm() { return ALGORITHM; }
+ @Override
+ public String getAlgorithm() {
+ return ALGORITHM;
+ }
}
\ No newline at end of file
diff --git a/bundleplugin/src/main/java/aQute/libg/cryptography/RSA.java b/bundleplugin/src/main/java/aQute/libg/cryptography/RSA.java
index 61bafb5..c1aa2ee 100644
--- a/bundleplugin/src/main/java/aQute/libg/cryptography/RSA.java
+++ b/bundleplugin/src/main/java/aQute/libg/cryptography/RSA.java
@@ -15,7 +15,8 @@
static private KeyFactory getKeyFactory() {
try {
return KeyFactory.getInstance(ALGORITHM);
- } catch (Exception e) {
+ }
+ catch (Exception e) {
// built in
}
return null;
@@ -24,20 +25,23 @@
public static RSAPrivateKey create(RSAPrivateKeySpec keyspec) throws InvalidKeySpecException {
return (RSAPrivateKey) factory.generatePrivate(keyspec);
}
+
public static RSAPublicKey create(RSAPublicKeySpec keyspec) throws InvalidKeySpecException {
return (RSAPublicKey) factory.generatePrivate(keyspec);
}
-
+
public static RSAPublicKey createPublic(BigInteger m, BigInteger e) throws InvalidKeySpecException {
- return create( new RSAPublicKeySpec(m,e));
+ return create(new RSAPublicKeySpec(m, e));
}
+
public static RSAPrivateKey createPrivate(BigInteger m, BigInteger e) throws InvalidKeySpecException {
- return create( new RSAPrivateKeySpec(m,e));
+ return create(new RSAPrivateKeySpec(m, e));
}
-
- public static Pair<RSAPrivateKey, RSAPublicKey> generate() throws NoSuchAlgorithmException {
+
+ public static Pair<RSAPrivateKey,RSAPublicKey> generate() throws NoSuchAlgorithmException {
KeyPairGenerator kpg = KeyPairGenerator.getInstance(ALGORITHM);
KeyPair keypair = kpg.generateKeyPair();
- return new Pair<RSAPrivateKey,RSAPublicKey>( (RSAPrivateKey) keypair.getPrivate(), (RSAPublicKey) keypair.getPublic());
+ return new Pair<RSAPrivateKey,RSAPublicKey>((RSAPrivateKey) keypair.getPrivate(),
+ (RSAPublicKey) keypair.getPublic());
}
}
diff --git a/bundleplugin/src/main/java/aQute/libg/cryptography/SHA1.java b/bundleplugin/src/main/java/aQute/libg/cryptography/SHA1.java
index a01f112..d0a486c 100644
--- a/bundleplugin/src/main/java/aQute/libg/cryptography/SHA1.java
+++ b/bundleplugin/src/main/java/aQute/libg/cryptography/SHA1.java
@@ -3,33 +3,36 @@
import java.io.*;
import java.security.*;
-
-
public class SHA1 extends Digest {
- public final static String ALGORITHM = "SHA1";
-
-
- public static Digester<SHA1> getDigester(OutputStream ... out ) throws NoSuchAlgorithmException {
+ public final static String ALGORITHM = "SHA1";
+
+ public static Digester<SHA1> getDigester(OutputStream... out) throws NoSuchAlgorithmException {
MessageDigest md = MessageDigest.getInstance(ALGORITHM);
return new Digester<SHA1>(md, out) {
- @Override public SHA1 digest() throws Exception {
+ @Override
+ public SHA1 digest() throws Exception {
return new SHA1(md.digest());
}
- @Override public SHA1 digest(byte[] bytes) {
+ @Override
+ public SHA1 digest(byte[] bytes) {
return new SHA1(bytes);
}
- @Override public String getAlgorithm() {
+
+ @Override
+ public String getAlgorithm() {
return ALGORITHM;
}
};
}
-
+
public SHA1(byte[] b) {
super(b, 20);
}
-
- @Override public String getAlgorithm() { return ALGORITHM; }
+ @Override
+ public String getAlgorithm() {
+ return ALGORITHM;
+ }
}
\ No newline at end of file
diff --git a/bundleplugin/src/main/java/aQute/libg/cryptography/SHA256.java b/bundleplugin/src/main/java/aQute/libg/cryptography/SHA256.java
index 5b25cf3..b6eeacc 100644
--- a/bundleplugin/src/main/java/aQute/libg/cryptography/SHA256.java
+++ b/bundleplugin/src/main/java/aQute/libg/cryptography/SHA256.java
@@ -3,33 +3,36 @@
import java.io.*;
import java.security.*;
-
-
public class SHA256 extends Digest {
- public final static String ALGORITHM = "SHA256";
-
-
- public static Digester<SHA256> getDigester(OutputStream ... out ) throws NoSuchAlgorithmException {
+ public final static String ALGORITHM = "SHA256";
+
+ public static Digester<SHA256> getDigester(OutputStream... out) throws NoSuchAlgorithmException {
MessageDigest md = MessageDigest.getInstance(ALGORITHM);
return new Digester<SHA256>(md, out) {
- @Override public SHA256 digest() throws Exception {
+ @Override
+ public SHA256 digest() throws Exception {
return new SHA256(md.digest());
}
- @Override public SHA256 digest(byte[] bytes) {
+ @Override
+ public SHA256 digest(byte[] bytes) {
return new SHA256(bytes);
}
- @Override public String getAlgorithm() {
+
+ @Override
+ public String getAlgorithm() {
return ALGORITHM;
}
};
}
-
+
public SHA256(byte[] b) {
super(b, 32);
}
-
- @Override public String getAlgorithm() { return ALGORITHM; }
+ @Override
+ public String getAlgorithm() {
+ return ALGORITHM;
+ }
}
\ No newline at end of file
diff --git a/bundleplugin/src/main/java/aQute/libg/cryptography/Signer.java b/bundleplugin/src/main/java/aQute/libg/cryptography/Signer.java
index 5218e2d..977eda6 100644
--- a/bundleplugin/src/main/java/aQute/libg/cryptography/Signer.java
+++ b/bundleplugin/src/main/java/aQute/libg/cryptography/Signer.java
@@ -5,36 +5,39 @@
public class Signer<D extends Digest> extends OutputStream {
Signature signature;
- Digester<D> digester;
-
+ Digester<D> digester;
+
Signer(Signature s, Digester<D> digester) {
this.signature = s;
- this.digester = digester;
+ this.digester = digester;
}
- @Override public void write(byte[] buffer, int offset, int length) throws IOException {
+ @Override
+ public void write(byte[] buffer, int offset, int length) throws IOException {
try {
signature.update(buffer, offset, length);
digester.write(buffer, offset, length);
- } catch (SignatureException e) {
+ }
+ catch (SignatureException e) {
throw new IOException(e.getLocalizedMessage());
}
}
- @Override public void write(int b) throws IOException {
+ @Override
+ public void write(int b) throws IOException {
try {
signature.update((byte) b);
digester.write(b);
- } catch (SignatureException e) {
+ }
+ catch (SignatureException e) {
throw new IOException(e.getLocalizedMessage());
}
}
-
public Signature signature() throws Exception {
return signature;
}
-
+
public D digest() throws Exception {
return digester.digest();
}
diff --git a/bundleplugin/src/main/java/aQute/libg/cryptography/Verifier.java b/bundleplugin/src/main/java/aQute/libg/cryptography/Verifier.java
index 5506ece..1005e0a 100644
--- a/bundleplugin/src/main/java/aQute/libg/cryptography/Verifier.java
+++ b/bundleplugin/src/main/java/aQute/libg/cryptography/Verifier.java
@@ -3,30 +3,31 @@
import java.io.*;
import java.security.*;
-
public class Verifier extends OutputStream {
- final Signature signature;
- final Digest d;
-
+ final Signature signature;
+ final Digest d;
+
Verifier(Signature s, Digest d) {
this.signature = s;
this.d = d;
}
-
+
@Override
- public void write( byte[] buffer, int offset, int length) throws IOException {
+ public void write(byte[] buffer, int offset, int length) throws IOException {
try {
signature.update(buffer, offset, length);
- } catch (SignatureException e) {
+ }
+ catch (SignatureException e) {
throw new IOException(e.getLocalizedMessage());
}
}
@Override
- public void write( int b) throws IOException {
+ public void write(int b) throws IOException {
try {
signature.update((byte) b);
- } catch (SignatureException e) {
+ }
+ catch (SignatureException e) {
throw new IOException(e.getLocalizedMessage());
}
}
@@ -34,5 +35,5 @@
public boolean verify() throws Exception {
return signature.verify(d.digest());
}
-
+
}