2 * To change this template, choose Tools | Templates
3 * and open the template in the editor.
8 import org.apidesign.api.security.*;
9 import java.nio.ByteBuffer;
10 import java.security.MessageDigest;
11 import java.util.Arrays;
12 import java.util.Random;
13 import org.junit.After;
14 import org.junit.BeforeClass;
15 import org.junit.Test;
16 import static org.junit.Assert.*;
18 /** Compares that the MessageDigest and Digest yield the same results for
21 * @author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
23 public class OldAPIToNewAPITest {
24 private static byte[] arr;
25 private static long time;
26 private static byte[] resOld;
27 private static byte[] resNew;
29 public OldAPIToNewAPITest() {
33 public static void setUp() {
34 time = System.currentTimeMillis();
35 Random r = new Random(time);
36 arr = new byte[r.nextInt(1024)];
41 public void tearDown() {
45 public void generateHashUsingMessageDigest() throws Exception {
46 // BEGIN: day.end.bridges.BridgeToOldRegister
47 // The java.security.Providers cannot be registered in META-INF/services
48 // that is why one needs to either configure various properties or
49 // make some dummy call that will initialize our bridge class.
50 // Then the bridge class registers itself as a MessageDigest provider
51 // in its constructor.
54 Digest initialize = Digest.getInstance("MD5");
55 // END: day.end.bridges.BridgeToOldRegister
57 MessageDigest md = MessageDigest.getInstance("cnt");
58 byte[] res = md.digest(arr);
64 public void generateHashUsingNewDigest() throws Exception {
65 Digest d = Digest.getInstance("cnt");
66 ByteBuffer bb = ByteBuffer.wrap(arr);
67 byte[] res = d.digest(bb);
74 public void compareTheHashes() throws Exception {
75 if (!Arrays.equals(resOld, resNew)) {
76 fail("Arrays are different:\n" + Arrays.toString(resOld) + "\n" + Arrays.toString(resNew));