Java Code Examples for java.security.NoSuchAlgorithmException.printStackTrace()

The following are Jave code examples for showing how to use printStackTrace() of the java.security.NoSuchAlgorithmException class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
+ Save this method
Example 1
Project: CodeInPython   File: quiz10Fragment.java   View Source Code Vote up 6 votes
public String md5(String s) {
    try {
        // Create MD5 Hash
        MessageDigest digest = java.security.MessageDigest.getInstance("MD5");
        digest.update(s.getBytes());
        byte messageDigest[] = digest.digest();

        // Create Hex String
        StringBuffer hexString = new StringBuffer();
        for (int i=0; i<messageDigest.length; i++)
            hexString.append(Integer.toHexString(0xFF & messageDigest[i]));
        return hexString.toString();

    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    }
    return "";
}
 
Example 2
Project: private-WeChat   File: SignUtil.java   View Source Code Vote up 6 votes
/**
 * 验证签名
 * 
 * @param signature
 * @param timestamp
 * @param nonce
 * @return
 */
public static boolean checkSignature(String signature, String timestamp,
        String nonce) {
    String[] arr = new String[] { token, timestamp, nonce };
    // 将token、timestamp、nonce三个参数进行字典序排序
    Arrays.sort(arr);
    StringBuilder content = new StringBuilder();
    for (int i = 0; i < arr.length; i++) {
        content.append(arr[i]);
    }
    MessageDigest md = null;
    String tmpStr = null;

    try {
        md = MessageDigest.getInstance("SHA-1");
        // 将三个参数字符串拼接成一个字符串进行sha1加密
        byte[] digest = md.digest(content.toString().getBytes());
        tmpStr = byteToStr(digest);
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    }

    content = null;
    // 将sha1加密后的字符串可与signature对比,标识该请求来源于微信
    return tmpStr != null ? tmpStr.equals(signature.toUpperCase()) : false;
}
 
Example 3
Project: android-lite-utils   File: SHA512Utils.java   View Source Code Vote up 6 votes
/**
 * SHA-512 加密
 * StringBuffer支持并发操作,线性安全的,适合多线程中使用。
 *
 * @param str
 * @return
 */
public String encryptSHAWithBuffer(String str) {
    if (str == null || str.length() == 0) {
        return null;
    }
    StringBuffer buffer = new StringBuffer();
    try {
        MessageDigest digest = MessageDigest.getInstance("SHA-512");
        digest.update(str.getBytes());
        byte[] cipher = digest.digest();

        for (byte b : cipher) {
            String hexStr = Integer.toHexString(b & 0xff);
            buffer.append(hexStr.length() == 1 ? "0" + hexStr : hexStr);
        }
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    }
    return buffer.toString();
}
 
Example 4
Project: CodeInPython   File: quiz6Fragment.java   View Source Code Vote up 6 votes
public String md5(String s) {
    try {
        // Create MD5 Hash
        MessageDigest digest = java.security.MessageDigest.getInstance("MD5");
        digest.update(s.getBytes());
        byte messageDigest[] = digest.digest();

        // Create Hex String
        StringBuffer hexString = new StringBuffer();
        for (int i=0; i<messageDigest.length; i++)
            hexString.append(Integer.toHexString(0xFF & messageDigest[i]));
        return hexString.toString();

    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    }
    return "";
}
 
Example 5
Project: CloudDB   File: ConsistentHashing.java   View Source Code Vote up 6 votes
public static synchronized String getHashedKey(String key){
	try {
		md  = MessageDigest.getInstance("MD5");
	} catch (NoSuchAlgorithmException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}

	md.reset();
	md.update((key).getBytes());
	byte[] result = md.digest();
	String hashedkey = "";
	for(int i = 0; i < 16; i++){
		int n = result[i] >> 4 & 0x0F;
	 	hashedkey += String.format("%1s", Integer.toHexString(n));
	 	n = result[i] & 0xF;
	 	hashedkey += String.format("%1s", Integer.toHexString(n));
	 }
	return hashedkey;
}
 
Example 6
Project: letv   File: Device.java   View Source Code Vote up 5 votes
public static byte[] MD5(byte[] input) {
    MessageDigest md = null;
    try {
        md = MessageDigest.getInstance(CommonUtils.MD5_INSTANCE);
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    }
    if (md == null) {
        return null;
    }
    md.update(input);
    return md.digest();
}
 
Example 7
Project: labtablet   File: Utils.java   View Source Code Vote up 5 votes
public static String getMD5EncryptedString(String encTarget){
    MessageDigest mdEnc = null;
    try {
        mdEnc = MessageDigest.getInstance("MD5");
    } catch (NoSuchAlgorithmException e) {
        System.out.println("Exception while encrypting to md5");
        e.printStackTrace();
    } // Encryption algorithm
    mdEnc.update(encTarget.getBytes(), 0, encTarget.length());
    String md5 = new BigInteger(1, mdEnc.digest()).toString(16);
    while ( md5.length() < 32 ) {
        md5 = "0"+md5;
    }
    return md5;
}
 
Example 8
Project: webside   File: MD5.java   View Source Code Vote up 5 votes
/**
 * MD5加密
 * @param str 待加密的字符串
 * @return
 */
public static String encrypt(String str) {
    String result = null;
    try {
    	result = new String(str);
        MessageDigest md = MessageDigest.getInstance("MD5");
        result = byteToString(md.digest(str.getBytes()));
    } catch (NoSuchAlgorithmException ex) {
        ex.printStackTrace();
    }
    return result;
}
 
Example 9
Project: AndroidUtilCode-master   File: EncryptUtils.java   View Source Code Vote up 5 votes
/**
 * hash加密模板
 *
 * @param data      数据
 * @param algorithm 加密算法
 * @return 密文字节数组
 */
private static byte[] hashTemplate(byte[] data, 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;
    }
}
 
Example 10
Project: PCloud_Server_v3   File: MD5.java   View Source Code Vote up 5 votes
/**
 * 获取字符串的MD5
 * @param str	源字符串
 * @return
 */
public static String getMD5CodeByString(String str) {  
       try {  
           MessageDigest md;  
           md = MessageDigest.getInstance("MD5");  
           byte buf[] = md.digest(str.getBytes());  
           BASE64Encoder encoder = new BASE64Encoder();  
           return encoder.encode(buf);  
       } catch (NoSuchAlgorithmException e) {  
           e.printStackTrace();  
           return null;  
       }  
   }
 
Example 11
Project: ViolenciaContraMulher   File: Criptografia.java   View Source Code Vote up 5 votes
public static String md5(String senha) {
    try {
        MessageDigest md = MessageDigest.getInstance("MD5");
        BigInteger hash = new BigInteger(1, md.digest(senha.getBytes()));
        return ((String) hash.toString(16));
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    }
    return "";
}
 
Example 12
Project: sealtalk-android-master   File: MD5.java   View Source Code Vote up 5 votes
/**
 * MD5加密
 * @param str 待加密的字符串
 * @param lowerCase 大小写
 * @return
 */
public static String encrypt(String str, boolean lowerCase) {
    String result = null;
    try {
        result = str;
        MessageDigest md = MessageDigest.getInstance("MD5");
        result = byteToString(md.digest(str.getBytes()));
        if (lowerCase) {
            result = result.toLowerCase();
        }
    } catch (NoSuchAlgorithmException ex) {
        ex.printStackTrace();
    }
    return result;
}
 
Example 13
Project: rongyunDemo   File: MD5.java   View Source Code Vote up 5 votes
/**
 * MD5加密
 * @param str 待加密的字符串
 * @param lowerCase 大小写
 * @return
 */
public static String encrypt(String str, boolean lowerCase) {
    String result = null;
    try {
        result = str;
        MessageDigest md = MessageDigest.getInstance("MD5");
        result = byteToString(md.digest(str.getBytes()));
        if (lowerCase) {
            result = result.toLowerCase();
        }
    } catch (NoSuchAlgorithmException ex) {
        ex.printStackTrace();
    }
    return result;
}
 
Example 14
Project: RLibrary   File: MD5Utils.java   View Source Code Vote up 5 votes
private static MessageDigest getMD5MessageDigest(){
    try {
        return MessageDigest.getInstance("MD5");
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
        Log.e(TAG, "Exception while getting digest", e);
        return null;
    }
}
 
Example 15
Project: sealtalk-android-master   File: MD5.java   View Source Code Vote up 5 votes
/**
 * MD5加密
 * @param str 待加密的字符串
 * @return
 */
public static String encrypt(String str) {
    String result = null;
    try {
        result = str;
        MessageDigest md = MessageDigest.getInstance("MD5");
        result = byteToString(md.digest(str.getBytes()));
    } catch (NoSuchAlgorithmException ex) {
        ex.printStackTrace();
    }
    return result;
}
 
Example 16
Project: mDL-ILP   File: CardIOUtils.java   View Source Code Vote up 5 votes
public static byte[] calculateKSessionMac(byte[] kSessionSeed) {
    MessageDigest md;
    try {
        md = MessageDigest.getInstance("SHA-1");
        // KSession_MAC  = the first 16 bytes of the SHA-1 hash over the concatenation of KSession_Seed ++ �00 00 00 02� with the parity bits adjusted
        byte[] unadjustedParityKmac = Arrays.copyOfRange(md.digest(Bytes.concatenate(kSessionSeed, new byte[] {0x00, 0x00, 0x00, 0x02})), 0, 16);
        byte[] kmac = adjustParityBits(unadjustedParityKmac);
        return kmac;
    } catch (NoSuchAlgorithmException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return null;
}
 
Example 17
Project: CS4160-trustchain-android   File: TrustChainBlock.java   View Source Code Vote up 5 votes
/**
 * Returns a sha256 hash of the block.
 * @param block - a proto Trustchain block
 * @return the sha256 hash of the byte array of the block
 */
public static byte[] hash(MessageProto.TrustChainBlock block) {
    MessageDigest md = null;
    try {
        md = MessageDigest.getInstance("SHA-256");
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    }
    //remove the signature (if there is any)
    MessageProto.TrustChainBlock rawBlock = block.toBuilder().setSignature(EMPTY_SIG).build();
    return md.digest(rawBlock.toByteArray());
}
 
Example 18
Project: messages-java-sdk   File: TestHelper.java   View Source Code Vote up 5 votes
/**
 * Get SHA1 hash of a string
 * @param convertme The string to convert
 * @return SHA1 hash
 */
public static String toSHA1(String convertme) 
{
    byte[] data = convertme.getBytes();
    MessageDigest md = null;
    try {
        md = MessageDigest.getInstance("SHA-1");
    }
    catch(NoSuchAlgorithmException e) {
        e.printStackTrace();
    } 
    return byteArrayToHexString(md.digest(data));
}
 
Example 19
Project: kettle_support_kettle8.0   File: Random.java   View Source Code Vote up 5 votes
public static String sha1(String value) {
	try {
		return hash(MessageDigest.getInstance(SHA1), value);
	} catch (NoSuchAlgorithmException e) {
		e.printStackTrace();
	}
	return null;
}
 
Example 20
Project: skLib   File: ExprSHA256Hash.java   View Source Code Vote up 5 votes
@Override
@Nullable
protected String[] get(Event arg0) {
	String s = string.getSingle(arg0);
	MessageDigest m = null;
	try {
		m = MessageDigest.getInstance("SHA-256");
		m.update(s.getBytes(),0,s.length());
	} catch (NoSuchAlgorithmException e) {
		e.printStackTrace();
	}
	return new String[] {new BigInteger(1,m.digest()).toString(16)};
}