Skip to content

Instantly share code, notes, and snippets.

@jsphdnl
Created December 28, 2017 15:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jsphdnl/c82e678a3aca5c9d9c796709e0e9fa30 to your computer and use it in GitHub Desktop.
Save jsphdnl/c82e678a3aca5c9d9c796709e0e9fa30 to your computer and use it in GitHub Desktop.
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Base64;
class SHA1Gen1 {
public static final String SHA1 = "SHA-1";
/**
* Encode data in base64 string
* @param data data in bytes
* @return a string in base64 format
*/
public static String base64Encode(byte[] data) {
Base64.Encoder encoder = Base64.getEncoder();
return encoder.encodeToString(data);
}
/**
* Generate SHA1 hash from a text
* @param data the text data to be hashed
* @return a string in base64 encoded format
* @throws java.security.NoSuchAlgorithmException
*/
public static String generateSHA1Hash(String data) throws NoSuchAlgorithmException {
MessageDigest messageDigest = MessageDigest.getInstance(SHA1);
messageDigest.update(data.getBytes());
byte [] hash = messageDigest.digest();
messageDigest.reset();
return base64Encode(hash);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment