30

I am trying to get MD5 encrypted pass from MySQL DB via Java code (Hibernate). But I cant get neither String nor any reasonable Java type.

The only thing I am getting is this unhelpful message: java.lang.ClassCastException: [B cannot be cast to com.mysql.jdbc.Blob (or whatever Java type I try cast to).

Here is my method:

public void testCrypto() {
        session.beginTransaction();
        // creates native SQL query
        // uses native MySQL's MD5 crypto
        final Blob pass = (Blob) session.createSQLQuery("SELECT MD5('somePass')")
            .list().get(0);
        session.getTransaction().commit();
}

Here is full stack trace:

java.lang.ClassCastException: [B cannot be cast to com.mysql.jdbc.Blob
    at domain.DatabaseTest.testCrypto(DatabaseTest.java:57)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at junit.framework.TestCase.runTest(TestCase.java:168)
    at junit.framework.TestCase.runBare(TestCase.java:134)
    at junit.framework.TestResult$1.protect(TestResult.java:110)
    at junit.framework.TestResult.runProtected(TestResult.java:128)
    at junit.framework.TestResult.run(TestResult.java:113)
    at junit.framework.TestCase.run(TestCase.java:124)
    at junit.framework.TestSuite.runTest(TestSuite.java:232)
    at junit.framework.TestSuite.run(TestSuite.java:227)
    at org.junit.internal.runners.JUnit38ClassRunner.run(JUnit38ClassRunner.java:83)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:49)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
Scott Arciszewski
  • 33,610
  • 16
  • 89
  • 206
Xorty
  • 18,367
  • 27
  • 104
  • 155

4 Answers4

59

That my friend is an array of bytes. In JNI, [B is used to describe an array ([) of bytes (B). An array of ints is [I etc.

You can get a bit more information on field descriptors here:
JNI Types and Data Structures (Table 3-2 should be what you are looking for).

Bruno Ferreira
  • 1,621
  • 12
  • 19
rfeak
  • 8,124
  • 29
  • 28
9

It's the class name of byte[].class. Try this:

System.out.println(byte[].class.getName());

Output (you guessed it):

[B

And if you want to access the readable name, use Class.getCanonicalName():

System.out.println(byte[].class.getCanonicalName());

Output:

byte[]

Sean Patrick Floyd
  • 292,901
  • 67
  • 465
  • 588
4

As the other answers state, that is a byte array.

If you want to get a string from a byte array, use the String constructor:

public void testCrypto()
{
        session.beginTransaction();
        // creates native SQL query
        // uses native MySQL's MD5 crypto
        final String pass = new String(session.createSQLQuery("SELECT MD5('somePass')")
            .list().get(0));
        session.getTransaction().commit();
}
Evan Mulawski
  • 54,662
  • 15
  • 117
  • 144
  • great, but this is really strange behavior! This String is exact same as what prints mysql console. But pass.getBytes().length says its 32, and MD5 should be 128bits. Where did I lost clue? – Xorty Jan 05 '11 at 17:21
  • 1
    @Xorty: There is a difference between measuring encryption strength and the length of a String. MD5 will convert any input to a string that has no more and no less than 32 hex characters. – Evan Mulawski Jan 05 '11 at 17:25
3

[B is the encoded type name for a byte array (byte[]), which should normally only appear in type signature strings, as its not a valid type name.

Chris Dodd
  • 119,907
  • 13
  • 134
  • 226