2011-01-05 5 views
17

Próbuję uzyskać zaszyfrowane hasło MD5 z MySQL DB poprzez kod Java (Hibernacja). Ale nie mogę uzyskać ani String, ani żadnego rozsądnego typu Java.Jaki rodzaj Java jest "[B"?

Jedyne co otrzymuję jest to bezużyteczne wiadomość: java.lang.ClassCastException: [B nie mogą być oddane do com.mysql.jdbc.Blob (lub cokolwiek typ Java Próbuję rzucić się).

Oto moja metoda:

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(); 
} 

Oto pełna ślad stosu:

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) 

Odpowiedz

3

to kodowana nazwa typu tablicy bajtów (bajt []), która powinna normalnie występować tylko w łańcuchach znaków typu, ponieważ nie jest poprawną nazwą typu.

6

To jest nazwa klasy byte[].class. Spróbuj tego:

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

Output (zgadliście):

[B

A jeśli chcesz uzyskać dostęp do nazwy czytelny, korzystania Class.getCanonicalName():

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

Wyjście:

bajt []

4

Jako stan innych odpowiedzi, która jest tablicą bajtów.

Jeśli chcesz uzyskać ciąg z tablicy bajtów, należy użyć konstruktora String:

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(); 
} 
+0

świetnie, ale to naprawdę dziwne zachowanie! Ten ciąg jest dokładnie taki sam, jak wypisuje konsolę mysql. Ale pass.getBytes(). Length mówi 32, a MD5 powinno być 128-bitowe. Gdzie zgubiłem trop? – Xorty

+1

@Xorty: Istnieje różnica między zmierzeniem siły szyfrowania a długością łańcucha. MD5 zamieni dowolne dane wejściowe na ciąg znaków, który nie ma więcej niż 32 znaków szesnastkowych. –

+0

Świetne, mit odpadł;) – Xorty