Pesquisar neste blogue

quinta-feira, 5 de abril de 2018

How to convert Pem certificate to Hex string in Java (a stackoverflow conglumerate)


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
//*******************************************************************
// Welcome to CompileJava!
// If you experience any issues, please contact us ('More Info')  -->
//*******************************************************************

import java.lang.Math; // headers MUST be above the first class
import java.util.Base64;

// one class needs to have a main() method
public class HelloWorld
{
  public static String toHexString( byte[] bytes )
  {
      StringBuffer sb = new StringBuffer( bytes.length*2 );
      for( int i = 0; i < bytes.length; i++ )
      {
          sb.append( toHex(bytes[i] >> 4) );
          sb.append( toHex(bytes[i]) );
      }

      return sb.toString();
  }
  private static char toHex(int nibble)
  {
      final char[] hexDigit =
      {
          '0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'
      };
      return hexDigit[nibble & 0xF];
  }
  // arguments are passed using the text field below this editor
  public static void main(String[] args)
  {
    String pemText="-----BEGIN CERTIFICATE-----"+
"blablabla"+
"-----END CERTIFICATE-----";
   byte[] certificateBytes = Base64.getDecoder().decode(
    pemText.replaceAll("-----(BEGIN|END) CERTIFICATE-----", "").replaceAll("\n", "").getBytes()
);
    System.out.println(toHexString(certificateBytes));
  }
}

Sem comentários:

Enviar um comentário