# HG changeset patch # User Jaroslav Tulach # Date 1213429941 -7200 # Node ID d4dd8d7df7a2df3857b58833adeb0ac17f529481 # Parent 8f9ed9c4a97b9af1d019fdc694b21e4054384123 making the delegation to the old API work without any problems diff -r 8f9ed9c4a97b -r d4dd8d7df7a2 samples/messagedigest/src/META-INF/services/impl.friendapi.DigestProvider --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/samples/messagedigest/src/META-INF/services/impl.friendapi.DigestProvider Sat Jun 14 09:52:21 2008 +0200 @@ -0,0 +1,2 @@ +# this shall be in a separate module that provides the "bridge" +impl.independent.module.BridgeToOldAlgorithmsProvider diff -r 8f9ed9c4a97b -r d4dd8d7df7a2 samples/messagedigest/src/api/Digest.java --- a/samples/messagedigest/src/api/Digest.java Sat Jun 14 09:51:46 2008 +0200 +++ b/samples/messagedigest/src/api/Digest.java Sat Jun 14 09:52:21 2008 +0200 @@ -2,7 +2,9 @@ package api; import java.nio.ByteBuffer; -import spi.DigestImplementation; +import impl.friendapi.DigestImplementation; +import impl.friendapi.DigestProvider; +import java.util.ServiceLoader; /** MessageDigest extends MessageDigestSpi, that means the javadoc * @@ -19,7 +21,13 @@ /** Factory method to create digest for an algorithm. */ public static Digest getInstance(String algorithm) { - return null; + for (DigestProvider dp : ServiceLoader.load(DigestProvider.class)) { + DigestImplementation impl = dp.create(algorithm); + if (impl != null) { + return new Digest(impl); + } + } + throw new IllegalArgumentException(algorithm); } // @@ -28,7 +36,7 @@ // public byte[] digest(ByteBuffer bb) { - return null; + impl.update(bb); + return impl.digest(); } - } diff -r 8f9ed9c4a97b -r d4dd8d7df7a2 samples/messagedigest/src/impl/friendapi/DigestImplementation.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/samples/messagedigest/src/impl/friendapi/DigestImplementation.java Sat Jun 14 09:52:21 2008 +0200 @@ -0,0 +1,23 @@ +/* + * To change this template, choose Tools | Templates + * and open the template in the editor. + */ + +package impl.friendapi; + +import java.nio.ByteBuffer; + +/** + * + * @author Jaroslav Tulach + */ +public abstract class DigestImplementation { + final String name; + + protected DigestImplementation(String algorithm) { + this.name = algorithm; + } + + public abstract void update(ByteBuffer bb); + public abstract byte[] digest(); +} diff -r 8f9ed9c4a97b -r d4dd8d7df7a2 samples/messagedigest/src/impl/friendapi/DigestProvider.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/samples/messagedigest/src/impl/friendapi/DigestProvider.java Sat Jun 14 09:52:21 2008 +0200 @@ -0,0 +1,14 @@ +/* + * To change this template, choose Tools | Templates + * and open the template in the editor. + */ + +package impl.friendapi; + +/** + * + * @author Jaroslav Tulach + */ +public interface DigestProvider { + public abstract DigestImplementation create(String algorithm); +} diff -r 8f9ed9c4a97b -r d4dd8d7df7a2 samples/messagedigest/src/impl/independent/module/BridgeToOldAlgorithmsProvider.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/samples/messagedigest/src/impl/independent/module/BridgeToOldAlgorithmsProvider.java Sat Jun 14 09:52:21 2008 +0200 @@ -0,0 +1,43 @@ +/* + * To change this template, choose Tools | Templates + * and open the template in the editor. + */ + +package impl.independent.module; + +import impl.friendapi.DigestImplementation; +import impl.friendapi.DigestProvider; +import java.nio.ByteBuffer; +import java.security.MessageDigest; +import java.security.NoSuchAlgorithmException; +import java.util.logging.Level; +import java.util.logging.Logger; + +/** + * + * @author Jaroslav Tulach + */ +public class BridgeToOldAlgorithmsProvider implements DigestProvider { + + public DigestImplementation create(String algorithm) { + try { + final MessageDigest md = MessageDigest.getInstance(algorithm); + return new DigestImplementation(algorithm) { + + @Override + public void update(ByteBuffer bb) { + md.update(bb); + } + + @Override + public byte[] digest() { + return md.digest(); + } + }; + } catch (NoSuchAlgorithmException ex) { + Logger.getLogger(BridgeToOldAlgorithmsProvider.class.getName()).log(Level.INFO, null, ex); + return null; + } + } + +} diff -r 8f9ed9c4a97b -r d4dd8d7df7a2 samples/messagedigest/test/api/DigestTest.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/samples/messagedigest/test/api/DigestTest.java Sat Jun 14 09:52:21 2008 +0200 @@ -0,0 +1,65 @@ +/* + * To change this template, choose Tools | Templates + * and open the template in the editor. + */ + +package api; + +import api.Digest; +import java.nio.ByteBuffer; +import java.security.MessageDigest; +import java.util.Arrays; +import java.util.Random; +import org.junit.After; +import org.junit.BeforeClass; +import org.junit.Test; +import static org.junit.Assert.*; + +/** Compares that the MessageDigest and Digest yield the same results for + * default provider. + * + * @author Jaroslav Tulach + */ +public class DigestTest { + private static byte[] arr; + private static long time; + private static byte[] resOld; + private static byte[] resNew; + + public DigestTest() { + } + + @BeforeClass + public static void setUp() { + time = System.currentTimeMillis(); + Random r = new Random(time); + arr = new byte[r.nextInt(1024 * 1024)]; + r.nextBytes(arr); + } + + @After + public void tearDown() { + } + + @Test + public void generateHashUsingMessageDigest() throws Exception { + MessageDigest md = MessageDigest.getInstance("MD5"); + byte[] res = md.digest(arr); + resOld = res; + } + + @Test + public void generateHashUsingNewDigest() throws Exception { + Digest d = Digest.getInstance("MD5"); + ByteBuffer bb = ByteBuffer.wrap(arr); + byte[] res = d.digest(bb); + resNew = res; + } + + @Test + public void compareTheHashes() throws Exception { + if (!Arrays.equals(resOld, resNew)) { + fail("Arrays are different:\n" + Arrays.toString(resOld) + "\n" + Arrays.toString(resNew)); + } + } +} \ No newline at end of file