This page presents a few examples of using Java for passwords and hashing.

Generate Password

A Java example to generate a random password containing only alphanumeric characters.

private static SecureRandom random = new SecureRandom();
private static final String STR_ALPHA = "abcdefghijklmnopqrstuvwxyz";
private static final String STR_ALL = STR_ALPHA + STR_ALPHA.toUpperCase() + "0123456789";
private static final char [] ALL_CHARS = STR_ALL. toCharArray();


public final char[] generatePassword(int length){
  char[] result = new char[length];
  for(int i=0; i<length; i++){
    int index = random.nextInt(ALL_CHARS.length);
    result[i] = ALL_CHARS[index];
  }
  return result;
}

Java Code to ComputeSHA-512 digest

public static String computeSha512(String input){
  MessageDigest digest;
  try{
    digest = MessageDigest.getInstance("SHA-512");
    byte[] bytes = digest.digest(input.getBytes());
    BigInteger digestAsNumber = new BigInteger(1,digest);
    StringBuilder sb = new StringBuilder( number.toString(16) );
    // padding to 32 hex digits
    while(sb.length()<32){
      sb.insert(0,"0");
    }
    return sb.toString();
  } catch (NoSuchAlgorithmException e){
    throw new IllegalStateException("provided algorithm must exists");
  }
}

other..

linux get sha-512/


0 Comments

Leave a Reply

Avatar placeholder

Your email address will not be published. Required fields are marked *