Java SHA256加密

private static final String strType = "SHA-256";
/***
* 字符串 SHA 加密**
* 
 @param strSourceText
	 * @return
	 */
	public static byte[] SHA(final String strText) {
		// 是否是有效字符串
		if (strText != null && strText.length() > 0) {
			try {
				// SHA 加密开始
				// 创建加密对象 并傳入加密類型
				MessageDigest messageDigest = MessageDigest.getInstance(strType);
				// 传入要加密的字符串
				messageDigest.update(strText.getBytes());
				// 得到 byte 類型结果
				return messageDigest.digest();

			} catch (NoSuchAlgorithmException e) {
				return null;
			}
		}else{
			return null;
		}

	}

猜你喜欢

转载自blog.csdn.net/qq_28827039/article/details/80335528