use of java.security.NoSuchAlgorithmException in project weixin-java-tools by chanjarster.
the class WxCpServiceImpl method createJsapiSignature.
public WxJsapiSignature createJsapiSignature(String url) throws WxErrorException {
long timestamp = System.currentTimeMillis() / 1000;
String noncestr = RandomUtils.getRandomStr();
String jsapiTicket = getJsapiTicket(false);
try {
String signature = SHA1.genWithAmple("jsapi_ticket=" + jsapiTicket, "noncestr=" + noncestr, "timestamp=" + timestamp, "url=" + url);
WxJsapiSignature jsapiSignature = new WxJsapiSignature();
jsapiSignature.setTimestamp(timestamp);
jsapiSignature.setNoncestr(noncestr);
jsapiSignature.setUrl(url);
jsapiSignature.setSignature(signature);
return jsapiSignature;
} catch (NoSuchAlgorithmException e) {
throw new RuntimeException(e);
}
}
use of java.security.NoSuchAlgorithmException in project checkstyle by checkstyle.
the class PropertyCacheFileTest method testExceptionNoSuchAlgorithmException.
@Test
@SuppressWarnings("unchecked")
public void testExceptionNoSuchAlgorithmException() throws Exception {
final Configuration config = new DefaultConfiguration("myName");
final String filePath = temporaryFolder.newFile().getPath();
final PropertyCacheFile cache = new PropertyCacheFile(config, filePath);
cache.put("myFile", 1);
mockStatic(MessageDigest.class);
when(MessageDigest.getInstance("SHA-1")).thenThrow(NoSuchAlgorithmException.class);
final Class<?>[] param = new Class<?>[1];
param[0] = Serializable.class;
final Method method = PropertyCacheFile.class.getDeclaredMethod("getHashCodeBasedOnObjectContent", param);
method.setAccessible(true);
try {
method.invoke(cache, config);
fail("InvocationTargetException is expected");
} catch (InvocationTargetException ex) {
assertTrue(ex.getCause().getCause() instanceof NoSuchAlgorithmException);
assertEquals("Unable to calculate hashcode.", ex.getCause().getMessage());
}
}
use of java.security.NoSuchAlgorithmException in project UltimateAndroid by cymcsg.
the class CryptographyUtils method getCryptography.
public static String getCryptography(String content, String encryptType) throws NoSuchAlgorithmException {
try {
MessageDigest messageDigest = MessageDigest.getInstance(encryptType);
byte[] inputByteArray = content.getBytes();
messageDigest.update(inputByteArray);
byte[] resultByteArray = messageDigest.digest();
return byteArrayToHex(resultByteArray);
} catch (NoSuchAlgorithmException e) {
throw e;
}
}
use of java.security.NoSuchAlgorithmException in project UltimateAndroid by cymcsg.
the class CryptographyUtils method getMd5FromFile.
/**
* Get the MD5 of the file
*
* @param filePath
* @return
* @throws IOException
* @throws NoSuchAlgorithmException
*/
public static String getMd5FromFile(String filePath) throws IOException, NoSuchAlgorithmException {
int bufferSize = 256 * 1024;
FileInputStream fileInputStream = null;
DigestInputStream digestInputStream = null;
try {
MessageDigest messageDigest = MessageDigest.getInstance("MD5");
fileInputStream = new FileInputStream(filePath);
digestInputStream = new DigestInputStream(fileInputStream, messageDigest);
byte[] buffer = new byte[bufferSize];
while (digestInputStream.read(buffer) > 0) ;
messageDigest = digestInputStream.getMessageDigest();
byte[] resultByteArray = messageDigest.digest();
return byteArrayToHex(resultByteArray);
} catch (NoSuchAlgorithmException e) {
throw e;
} finally {
if (digestInputStream != null) {
digestInputStream.close();
}
if (fileInputStream != null) {
fileInputStream.close();
}
}
}
use of java.security.NoSuchAlgorithmException in project UltimateAndroid by cymcsg.
the class CryptographyUtils method encryptAlgorithm.
public static byte[] encryptAlgorithm(byte[] data, String algorithm) {
try {
MessageDigest md = MessageDigest.getInstance(algorithm);
md.update(data);
return md.digest();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
return new byte[0];
}
Aggregations