Search in sources :

Example 21 with NoSuchAlgorithmException

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);
    }
}
Also used : WxJsapiSignature(me.chanjar.weixin.common.bean.WxJsapiSignature) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException)

Example 22 with NoSuchAlgorithmException

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());
    }
}
Also used : Configuration(com.puppycrawl.tools.checkstyle.api.Configuration) Method(java.lang.reflect.Method) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) InvocationTargetException(java.lang.reflect.InvocationTargetException) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 23 with NoSuchAlgorithmException

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;
    }
}
Also used : NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) MessageDigest(java.security.MessageDigest)

Example 24 with NoSuchAlgorithmException

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();
        }
    }
}
Also used : DigestInputStream(java.security.DigestInputStream) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) MessageDigest(java.security.MessageDigest)

Example 25 with NoSuchAlgorithmException

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];
}
Also used : NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) MessageDigest(java.security.MessageDigest)

Aggregations

NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)1403 MessageDigest (java.security.MessageDigest)548 IOException (java.io.IOException)328 InvalidKeyException (java.security.InvalidKeyException)242 KeyStoreException (java.security.KeyStoreException)168 InvalidKeySpecException (java.security.spec.InvalidKeySpecException)145 CertificateException (java.security.cert.CertificateException)138 UnsupportedEncodingException (java.io.UnsupportedEncodingException)131 KeyManagementException (java.security.KeyManagementException)105 KeyFactory (java.security.KeyFactory)96 NoSuchProviderException (java.security.NoSuchProviderException)93 InvalidAlgorithmParameterException (java.security.InvalidAlgorithmParameterException)89 NoSuchPaddingException (javax.crypto.NoSuchPaddingException)79 UnrecoverableKeyException (java.security.UnrecoverableKeyException)78 KeyStore (java.security.KeyStore)73 SecureRandom (java.security.SecureRandom)72 SSLContext (javax.net.ssl.SSLContext)72 X509EncodedKeySpec (java.security.spec.X509EncodedKeySpec)69 BadPaddingException (javax.crypto.BadPaddingException)69 Cipher (javax.crypto.Cipher)69