当前位置: 首页>>代码示例>>Java>>正文


Java MessageDigest.getInstance方法代码示例

本文整理汇总了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 "";
}
 
开发者ID:project-sunbird,项目名称:sunbird-utils,代码行数:24,代码来源:OneWayHashing.java

示例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();
}
 
开发者ID:cybershrapnel,项目名称:nanocheeze,代码行数:18,代码来源:Torrent3.java

示例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.");
    }
}
 
开发者ID:lambdalab-mirror,项目名称:jdk8u-jdk,代码行数:40,代码来源:CipherStreamClose.java

示例4: getMessageDigest

import java.security.MessageDigest; //导入方法依赖的package包/类
public MessageDigest getMessageDigest() {
  try {
    return MessageDigest.getInstance(algorithmName);
  } catch (NoSuchAlgorithmException e) {
    throw new AssertionError(e);
  }
}
 
开发者ID:paul-hammant,项目名称:googles-monorepo-demo,代码行数:8,代码来源:MessageDigestAlgorithmBenchmark.java

示例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);
    }
}
 
开发者ID:philipwhiuk,项目名称:q-mail,代码行数:48,代码来源:Authentication.java

示例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);
    }
}
 
开发者ID:slashchenxiaojun,项目名称:wall.e,代码行数:17,代码来源:CodeKit.java

示例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;
}
 
开发者ID:superkoh,项目名称:k-framework,代码行数:17,代码来源:MD5.java

示例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 "";
}
 
开发者ID:sundevin,项目名称:utilsLibrary,代码行数:18,代码来源:MD5Utils.java

示例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;
    }
}
 
开发者ID:jmpaaak,项目名称:EyeShopping,代码行数:11,代码来源:PackageManagerUtils.java

示例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());
}
 
开发者ID:zhoulifu,项目名称:ss-java,代码行数:13,代码来源:RC4MD5Crypto.java

示例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);
    }
}
 
开发者ID:fergarrui,项目名称:ethereum-bytecode-analyzer,代码行数:23,代码来源:HashUtil.java

示例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;
    }
}
 
开发者ID:KwalaGroup,项目名称:Android-Client,代码行数:16,代码来源:Tools.java

示例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));
}
 
开发者ID:monPlan,项目名称:springboot-spwa-gae-demo,代码行数:8,代码来源:EncoderTest.java

示例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;
}
 
开发者ID:penghuanliang,项目名称:Rxjava2.0Demo,代码行数:38,代码来源:MD5Util.java

示例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;
    }
}
 
开发者ID:interactiveservices,项目名称:utils-android,代码行数:14,代码来源:EncryptUtils.java


注:本文中的java.security.MessageDigest.getInstance方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。