1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/samples/messagedigest/src/META-INF/services/impl.friendapi.DigestProvider Sat Jun 14 09:52:21 2008 +0200
1.3 @@ -0,0 +1,2 @@
1.4 +# this shall be in a separate module that provides the "bridge"
1.5 +impl.independent.module.BridgeToOldAlgorithmsProvider
2.1 --- a/samples/messagedigest/src/api/Digest.java Sat Jun 14 09:51:46 2008 +0200
2.2 +++ b/samples/messagedigest/src/api/Digest.java Sat Jun 14 09:52:21 2008 +0200
2.3 @@ -2,7 +2,9 @@
2.4 package api;
2.5
2.6 import java.nio.ByteBuffer;
2.7 -import spi.DigestImplementation;
2.8 +import impl.friendapi.DigestImplementation;
2.9 +import impl.friendapi.DigestProvider;
2.10 +import java.util.ServiceLoader;
2.11
2.12 /** MessageDigest extends MessageDigestSpi, that means the javadoc
2.13 *
2.14 @@ -19,7 +21,13 @@
2.15 /** Factory method to create digest for an algorithm.
2.16 */
2.17 public static Digest getInstance(String algorithm) {
2.18 - return null;
2.19 + for (DigestProvider dp : ServiceLoader.load(DigestProvider.class)) {
2.20 + DigestImplementation impl = dp.create(algorithm);
2.21 + if (impl != null) {
2.22 + return new Digest(impl);
2.23 + }
2.24 + }
2.25 + throw new IllegalArgumentException(algorithm);
2.26 }
2.27
2.28 //
2.29 @@ -28,7 +36,7 @@
2.30 //
2.31
2.32 public byte[] digest(ByteBuffer bb) {
2.33 - return null;
2.34 + impl.update(bb);
2.35 + return impl.digest();
2.36 }
2.37 -
2.38 }
3.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
3.2 +++ b/samples/messagedigest/src/impl/friendapi/DigestImplementation.java Sat Jun 14 09:52:21 2008 +0200
3.3 @@ -0,0 +1,23 @@
3.4 +/*
3.5 + * To change this template, choose Tools | Templates
3.6 + * and open the template in the editor.
3.7 + */
3.8 +
3.9 +package impl.friendapi;
3.10 +
3.11 +import java.nio.ByteBuffer;
3.12 +
3.13 +/**
3.14 + *
3.15 + * @author Jaroslav Tulach
3.16 + */
3.17 +public abstract class DigestImplementation {
3.18 + final String name;
3.19 +
3.20 + protected DigestImplementation(String algorithm) {
3.21 + this.name = algorithm;
3.22 + }
3.23 +
3.24 + public abstract void update(ByteBuffer bb);
3.25 + public abstract byte[] digest();
3.26 +}
4.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
4.2 +++ b/samples/messagedigest/src/impl/friendapi/DigestProvider.java Sat Jun 14 09:52:21 2008 +0200
4.3 @@ -0,0 +1,14 @@
4.4 +/*
4.5 + * To change this template, choose Tools | Templates
4.6 + * and open the template in the editor.
4.7 + */
4.8 +
4.9 +package impl.friendapi;
4.10 +
4.11 +/**
4.12 + *
4.13 + * @author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
4.14 + */
4.15 +public interface DigestProvider {
4.16 + public abstract DigestImplementation create(String algorithm);
4.17 +}
5.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
5.2 +++ b/samples/messagedigest/src/impl/independent/module/BridgeToOldAlgorithmsProvider.java Sat Jun 14 09:52:21 2008 +0200
5.3 @@ -0,0 +1,43 @@
5.4 +/*
5.5 + * To change this template, choose Tools | Templates
5.6 + * and open the template in the editor.
5.7 + */
5.8 +
5.9 +package impl.independent.module;
5.10 +
5.11 +import impl.friendapi.DigestImplementation;
5.12 +import impl.friendapi.DigestProvider;
5.13 +import java.nio.ByteBuffer;
5.14 +import java.security.MessageDigest;
5.15 +import java.security.NoSuchAlgorithmException;
5.16 +import java.util.logging.Level;
5.17 +import java.util.logging.Logger;
5.18 +
5.19 +/**
5.20 + *
5.21 + * @author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
5.22 + */
5.23 +public class BridgeToOldAlgorithmsProvider implements DigestProvider {
5.24 +
5.25 + public DigestImplementation create(String algorithm) {
5.26 + try {
5.27 + final MessageDigest md = MessageDigest.getInstance(algorithm);
5.28 + return new DigestImplementation(algorithm) {
5.29 +
5.30 + @Override
5.31 + public void update(ByteBuffer bb) {
5.32 + md.update(bb);
5.33 + }
5.34 +
5.35 + @Override
5.36 + public byte[] digest() {
5.37 + return md.digest();
5.38 + }
5.39 + };
5.40 + } catch (NoSuchAlgorithmException ex) {
5.41 + Logger.getLogger(BridgeToOldAlgorithmsProvider.class.getName()).log(Level.INFO, null, ex);
5.42 + return null;
5.43 + }
5.44 + }
5.45 +
5.46 +}
6.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
6.2 +++ b/samples/messagedigest/test/api/DigestTest.java Sat Jun 14 09:52:21 2008 +0200
6.3 @@ -0,0 +1,65 @@
6.4 +/*
6.5 + * To change this template, choose Tools | Templates
6.6 + * and open the template in the editor.
6.7 + */
6.8 +
6.9 +package api;
6.10 +
6.11 +import api.Digest;
6.12 +import java.nio.ByteBuffer;
6.13 +import java.security.MessageDigest;
6.14 +import java.util.Arrays;
6.15 +import java.util.Random;
6.16 +import org.junit.After;
6.17 +import org.junit.BeforeClass;
6.18 +import org.junit.Test;
6.19 +import static org.junit.Assert.*;
6.20 +
6.21 +/** Compares that the MessageDigest and Digest yield the same results for
6.22 + * default provider.
6.23 + *
6.24 + * @author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
6.25 + */
6.26 +public class DigestTest {
6.27 + private static byte[] arr;
6.28 + private static long time;
6.29 + private static byte[] resOld;
6.30 + private static byte[] resNew;
6.31 +
6.32 + public DigestTest() {
6.33 + }
6.34 +
6.35 + @BeforeClass
6.36 + public static void setUp() {
6.37 + time = System.currentTimeMillis();
6.38 + Random r = new Random(time);
6.39 + arr = new byte[r.nextInt(1024 * 1024)];
6.40 + r.nextBytes(arr);
6.41 + }
6.42 +
6.43 + @After
6.44 + public void tearDown() {
6.45 + }
6.46 +
6.47 + @Test
6.48 + public void generateHashUsingMessageDigest() throws Exception {
6.49 + MessageDigest md = MessageDigest.getInstance("MD5");
6.50 + byte[] res = md.digest(arr);
6.51 + resOld = res;
6.52 + }
6.53 +
6.54 + @Test
6.55 + public void generateHashUsingNewDigest() throws Exception {
6.56 + Digest d = Digest.getInstance("MD5");
6.57 + ByteBuffer bb = ByteBuffer.wrap(arr);
6.58 + byte[] res = d.digest(bb);
6.59 + resNew = res;
6.60 + }
6.61 +
6.62 + @Test
6.63 + public void compareTheHashes() throws Exception {
6.64 + if (!Arrays.equals(resOld, resNew)) {
6.65 + fail("Arrays are different:\n" + Arrays.toString(resOld) + "\n" + Arrays.toString(resNew));
6.66 + }
6.67 + }
6.68 +}
6.69 \ No newline at end of file