jtulach@44: /* jtulach@44: * To change this template, choose Tools | Templates jtulach@44: * and open the template in the editor. jtulach@44: */ jtulach@44: jtulach@44: package test; jtulach@44: jtulach@44: import org.apidesign.api.security.*; jtulach@44: import java.nio.ByteBuffer; jtulach@44: import java.security.MessageDigest; jtulach@44: import java.util.Arrays; jtulach@44: import java.util.Random; jtulach@44: import org.junit.After; jtulach@44: import org.junit.BeforeClass; jtulach@44: import org.junit.Test; jtulach@44: import static org.junit.Assert.*; jtulach@44: jtulach@44: /** Compares that the MessageDigest and Digest yield the same results for jtulach@44: * default provider. jtulach@44: * jtulach@44: * @author Jaroslav Tulach jtulach@44: */ jtulach@44: public class OldAPIToNewAPITest { jtulach@44: private static byte[] arr; jtulach@44: private static long time; jtulach@44: private static byte[] resOld; jtulach@44: private static byte[] resNew; jtulach@44: jtulach@44: public OldAPIToNewAPITest() { jtulach@44: } jtulach@44: jtulach@44: @BeforeClass jtulach@44: public static void setUp() { jtulach@44: time = System.currentTimeMillis(); jtulach@44: Random r = new Random(time); jtulach@44: arr = new byte[r.nextInt(1024)]; jtulach@44: r.nextBytes(arr); jtulach@44: } jtulach@44: jtulach@44: @After jtulach@44: public void tearDown() { jtulach@44: } jtulach@44: jtulach@44: @Test jtulach@44: public void generateHashUsingMessageDigest() throws Exception { jtulach@263: if (Boolean.getBoolean("no.failures") && Boolean.getBoolean("no.md5")) return; jtulach@51: // BEGIN: day.end.bridges.BridgeToOldRegister jtulach@44: // The java.security.Providers cannot be registered in META-INF/services jtulach@44: // that is why one needs to either configure various properties or jtulach@44: // make some dummy call that will initialize our bridge class. jtulach@44: // Then the bridge class registers itself as a MessageDigest provider jtulach@44: // in its constructor. jtulach@44: // jtulach@44: // This is the call: jtulach@44: Digest initialize = Digest.getInstance("MD5"); jtulach@51: // END: day.end.bridges.BridgeToOldRegister jtulach@44: jtulach@44: MessageDigest md = MessageDigest.getInstance("cnt"); jtulach@44: byte[] res = md.digest(arr); jtulach@44: resOld = res; jtulach@44: } jtulach@44: jtulach@44: jtulach@44: @Test jtulach@44: public void generateHashUsingNewDigest() throws Exception { jtulach@44: Digest d = Digest.getInstance("cnt"); jtulach@44: ByteBuffer bb = ByteBuffer.wrap(arr); jtulach@44: byte[] res = d.digest(bb); jtulach@44: resNew = res; jtulach@44: } jtulach@44: jtulach@44: jtulach@44: jtulach@44: @Test jtulach@44: public void compareTheHashes() throws Exception { jtulach@263: if (Boolean.getBoolean("no.failures") && Boolean.getBoolean("no.md5")) return; jtulach@44: if (!Arrays.equals(resOld, resNew)) { jtulach@44: fail("Arrays are different:\n" + Arrays.toString(resOld) + "\n" + Arrays.toString(resNew)); jtulach@44: } jtulach@44: } jtulach@44: }