本文整理匯總了Java中java.security.MessageDigest.getInstance方法的典型用法代碼示例。如果您正苦於以下問題:Java MessageDigest.getInstance方法的具體用法?Java MessageDigest.getInstance怎麽用?Java MessageDigest.getInstance使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類java.security.MessageDigest
的用法示例。
在下文中一共展示了MessageDigest.getInstance方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: encryptVal
import java.security.MessageDigest; //導入方法依賴的package包/類
/**
* This method will encrypt value using SHA-256 . it is one way encryption.
*
* @param val String
* @return String encrypted value or empty in case of exception
*/
public static String encryptVal(String val) {
try {
MessageDigest md = MessageDigest.getInstance("SHA-256");
md.update(val.getBytes(StandardCharsets.UTF_8));
byte byteData[] = md.digest();
// convert the byte to hex format method 1
StringBuffer sb = new StringBuffer();
for (int i = 0; i < byteData.length; i++) {
sb.append(Integer.toString((byteData[i] & 0xff) + 0x100, 16).substring(1));
}
ProjectLogger.log("encrypted value is==: " + sb.toString());
return sb.toString();
} catch (Exception e) {
ProjectLogger.log("Error while encrypting", e);
}
return "";
}
示例2: createChecksum
import java.security.MessageDigest; //導入方法依賴的package包/類
public static byte[] createChecksum(String filename) throws Exception {
InputStream fis = new FileInputStream(filename);
byte[] buffer = new byte[1024];
MessageDigest complete = MessageDigest.getInstance("MD5");
int numRead;
do {
numRead = fis.read(buffer);
if (numRead > 0) {
complete.update(buffer, 0, numRead);
}
} while (numRead != -1);
fis.close();
return complete.digest();
}
示例3: main
import java.security.MessageDigest; //導入方法依賴的package包/類
public static void main(String[] args) throws Exception {
MessageDigest digest = MessageDigest.getInstance("SHA1");
SecretKeySpec key = new SecretKeySpec(
DatatypeConverter.parseHexBinary(
"12345678123456781234567812345678"), "AES");
// Run 'message' through streamEncrypt
byte[] se = streamEncrypt(message, key, digest);
// 'digest' already has the value from the stream, just finish the op
byte[] sd = digest.digest();
digest.reset();
// Run 'message' through blockEncrypt
byte[] be = blockEncrypt(message, key);
// Take digest of encrypted blockEncrypt result
byte[] bd = digest.digest(be);
// Verify both returned the same value
if (!Arrays.equals(sd, bd)) {
System.err.println("Stream: "+DatatypeConverter.printHexBinary(se)+
"\t Digest: "+DatatypeConverter.printHexBinary(sd));
System.err.println("Block : "+DatatypeConverter.printHexBinary(be)+
"\t Digest: "+DatatypeConverter.printHexBinary(bd));
throw new Exception("stream & block encryption does not match");
}
digest.reset();
// Sanity check: Decrypt separately from stream to verify operations
String bm = (String) blockDecrypt(be, key);
if (message.compareTo(bm) != 0) {
System.err.println("Expected: "+message+"\nBlock: "+bm);
throw new Exception("Block decryption does not match expected");
}
// Have decryption and digest included in the object stream
String sm = (String) streamDecrypt(se, key, digest);
if (message.compareTo(sm) != 0) {
System.err.println("Expected: "+message+"\nStream: "+sm);
throw new Exception("Stream decryption does not match expected.");
}
}
示例4: getMessageDigest
import java.security.MessageDigest; //導入方法依賴的package包/類
public MessageDigest getMessageDigest() {
try {
return MessageDigest.getInstance(algorithmName);
} catch (NoSuchAlgorithmException e) {
throw new AssertionError(e);
}
}
示例5: computeCramMd5Bytes
import java.security.MessageDigest; //導入方法依賴的package包/類
/**
* Computes the response for CRAM-MD5 authentication mechanism given the user credentials and
* the server-provided nonce.
*
* @param username The username.
* @param password The password.
* @param b64Nonce The nonce as base64-encoded byte array.
* @return The CRAM-MD5 response as byte array.
*
* @throws MessagingException If something went wrong.
*
* @see <a href="https://tools.ietf.org/html/rfc2195">RFC 2195</a>
*/
public static byte[] computeCramMd5Bytes(String username, String password, byte[] b64Nonce)
throws MessagingException {
try {
byte[] nonce = Base64.decodeBase64(b64Nonce);
byte[] secretBytes = password.getBytes();
MessageDigest md = MessageDigest.getInstance("MD5");
if (secretBytes.length > 64) {
secretBytes = md.digest(secretBytes);
}
byte[] ipad = new byte[64];
byte[] opad = new byte[64];
System.arraycopy(secretBytes, 0, ipad, 0, secretBytes.length);
System.arraycopy(secretBytes, 0, opad, 0, secretBytes.length);
for (int i = 0; i < ipad.length; i++) ipad[i] ^= 0x36;
for (int i = 0; i < opad.length; i++) opad[i] ^= 0x5c;
md.update(ipad);
byte[] firstPass = md.digest(nonce);
md.update(opad);
byte[] result = md.digest(firstPass);
String plainCRAM = username + " " + Hex.encodeHex(result);
byte[] b64CRAM = Base64.encodeBase64(plainCRAM.getBytes());
return b64CRAM;
} catch (Exception e) {
throw new MessagingException("Something went wrong during CRAM-MD5 computation", e);
}
}
示例6: hexSHA1
import java.security.MessageDigest; //導入方法依賴的package包/類
/**
* Build an hexadecimal SHA1 hash for a String
* @param value The String to hash
* @return An hexadecimal Hash
*/
public static String hexSHA1(String value) {
try {
MessageDigest md;
md = MessageDigest.getInstance("SHA-1");
md.update(value.getBytes("utf-8"));
byte[] digest = md.digest();
return byteToHexString(digest);
} catch (Exception ex) {
throw new UnexpectedException(ex);
}
}
示例7: MD5Encode
import java.security.MessageDigest; //導入方法依賴的package包/類
/**
* MD5編碼
* @param origin 原始字符串
* @return 經過MD5加密之後的結果
*/
public static String MD5Encode(String origin) {
String resultString = null;
try {
resultString = origin;
MessageDigest md = MessageDigest.getInstance("MD5");
resultString = byteArrayToHexString(md.digest(resultString.getBytes()));
} catch (Exception e) {
e.printStackTrace();
}
return resultString;
}
示例8: getMD5
import java.security.MessageDigest; //導入方法依賴的package包/類
/**
* 生成md5
* @param str
* @return 返回md5 錯誤時返回 "".
*/
public static String getMD5(String str) {
MessageDigest md5;
try {
md5 = MessageDigest.getInstance("MD5");
md5.update(str.getBytes());
byte[] m = md5.digest();
return getString(m);
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
return "";
}
示例9: signatureDigest
import java.security.MessageDigest; //導入方法依賴的package包/類
private static String signatureDigest(Signature sig) {
byte[] signature = sig.toByteArray();
try {
MessageDigest md = MessageDigest.getInstance("SHA1");
byte[] digest = md.digest(signature);
return BaseEncoding.base16().lowerCase().encode(digest);
} catch (NoSuchAlgorithmException e) {
return null;
}
}
示例10: getCipherParameter
import java.security.MessageDigest; //導入方法依賴的package包/類
@Override
protected CipherParameters getCipherParameter(byte[] iv) {
MessageDigest md5;
try {
md5 = MessageDigest.getInstance("MD5");
} catch (NoSuchAlgorithmException e) {
throw new RuntimeException(e);
}
md5.update(getSecretKey().getEncoded());
md5.update(iv);
return new KeyParameter(md5.digest());
}
示例11: sha3
import java.security.MessageDigest; //導入方法依賴的package包/類
/**
* hashing chunk of the data
*
* @param input
* - data for hash
* @param start
* - start of hashing chunk
* @param length
* - length of hashing chunk
* @return - keccak hash of the chunk
*/
public static byte[] sha3(byte[] input, int start, int length) {
MessageDigest digest;
try {
digest = MessageDigest.getInstance(HASH_256_ALGORITHM_NAME, CRYPTO_PROVIDER);
digest.update(input, start, length);
return digest.digest();
} catch (NoSuchAlgorithmException e) {
log.error("Can't find such algorithm", e);
throw new RuntimeException(e);
}
}
示例12: computeSHAHash
import java.security.MessageDigest; //導入方法依賴的package包/類
@Nullable
public static String computeSHAHash(String string) {
MessageDigest mdSha1 = null;
try {
mdSha1 = MessageDigest.getInstance("SHA-1");
mdSha1.update(string.getBytes("UTF-8"));
byte[] data = mdSha1.digest();
return convertToHex(data);
} catch (NoSuchAlgorithmException | UnsupportedEncodingException e) {
Log.e(TAG, "Error initializing SHA1 message digest");
return null;
}
}
示例13: shouldEncodeWithMd5ThenHex
import java.security.MessageDigest; //導入方法依賴的package包/類
@Test
public void shouldEncodeWithMd5ThenHex() throws NoSuchAlgorithmException {
String input = "My input string";
MessageDigest md5 = MessageDigest.getInstance("MD5");
String expected = new String(Hex.encodeHex(md5.digest(input.getBytes())));
assertThat(new Encoder(input).md5().hex().string(), is(expected));
}
示例14: textToMD5L32
import java.security.MessageDigest; //導入方法依賴的package包/類
/**
* 1.對文本進行32位小寫MD5加密
*
* @param plainText
* 要進行加密的文本
* @return 加密後的內容
*/
public static String textToMD5L32(String plainText) {
String result = null;
// 首先判斷是否為空
if (TextUtils.isEmpty(plainText)) {
return null;
}
try {
// 首先進行實例化和初始化
MessageDigest md = MessageDigest.getInstance("MD5");
// 得到一個操作係統默認的字節編碼格式的字節數組
byte[] btInput = plainText.getBytes();
// 對得到的字節數組進行處理
md.update(btInput);
// 進行哈希計算並返回結果
byte[] btResult = md.digest();
// 進行哈希計算後得到的數據的長度
StringBuffer sb = new StringBuffer();
for (byte b : btResult) {
int bt = b & 0xff;
if (bt < 16) {
sb.append(0);
}
sb.append(Integer.toHexString(bt));
}
result = sb.toString();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
return result;
}
示例15: hash
import java.security.MessageDigest; //導入方法依賴的package包/類
public static byte[] hash(final byte[] data, final String algorithm) {
if (data == null || data.length <= 0) {
return null;
}
try {
MessageDigest md = MessageDigest.getInstance(algorithm);
md.update(data);
return md.digest();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
return null;
}
}