document.write('
Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1.     public static String passwordToMd5(char[] password, boolean clearPassword) {
  2.         String md5 = null;
  3.         if (password != null) {
  4.             try {
  5.                 //Convertir la entrada a array de bytes, ya que así se requiere
  6.                 //  para poder codificarla a MD5. No se debe convertir a String
  7.                 //  por cuestiones de seguridad
  8.                 CharBuffer charBuffer = CharBuffer.wrap(password);
  9.                 ByteBuffer byteBuffer = Charset.forName("UTF-8").encode(charBuffer);
  10.                 byte[] bytesPassword = Arrays.copyOfRange(byteBuffer.array(),
  11.                         byteBuffer.position(), byteBuffer.limit());
  12.                
  13.                 //Realizar la codificación
  14.                 MessageDigest md = MessageDigest.getInstance("MD5");
  15.                 byte[] arrayBytesMd5 = md.digest(bytesPassword);
  16.  
  17.                 //it is recommended that the returned character array be cleared
  18.                 //  after use by setting each character to zero
  19.                 Arrays.fill(charBuffer.array(), \'\\u0000\'); // clear sensitive data
  20.                 Arrays.fill(byteBuffer.array(), (byte) 0); // clear sensitive data
  21.                 Arrays.fill(bytesPassword, (byte) 0);
  22.                 if(clearPassword) {
  23.                     Arrays.fill(password, \'\\u0000\');
  24.                 }
  25.  
  26.                 //Convertir a hexadecimal pasándolo a BigInteger
  27.                 BigInteger bigIntMd5 = new BigInteger(1, arrayBytesMd5);
  28.                 md5 = bigIntMd5.toString(16);
  29.  
  30.                 // Now we need to zero pad it if you actually want the full 32 chars.
  31.                 while (md5.length() < 32) {
  32.                     md5 = "0" + md5;
  33.                 }
  34.             } catch (NoSuchAlgorithmException ex) {
  35.                 Logger.getLogger(MyClass.class.getName()).log(Level.SEVERE, null, ex);
  36.             }
  37.         }
  38.         return md5;
  39.     }
');