Search in sources :

Example 46 with NoSuchAlgorithmException

use of java.security.NoSuchAlgorithmException in project Anki-Android by Ramblurr.

the class Utils method convertStreamToString.

//    /**
//     * MD5 sum of file.
//     * Equivalent to checksum(open(os.path.join(mdir, file), "rb").read()))
//     *
//     * @param path The full path to the file
//     * @return A string of length 32 containing the hexadecimal representation of the MD5 checksum of the contents
//     * of the file
//     */
//    public static String fileChecksum(String path) {
//        byte[] bytes = null;
//        try {
//            File file = new File(path);
//            if (file != null && file.isFile()) {
//                bytes = new byte[(int)file.length()];
//                FileInputStream fin = new FileInputStream(file);
//                fin.read(bytes);
//            }
//        } catch (FileNotFoundException e) {
//            Log.e(AnkiDroidApp.TAG, "Can't find file " + path + " to calculate its checksum");
//        } catch (IOException e) {
//            Log.e(AnkiDroidApp.TAG, "Can't read file " + path + " to calculate its checksum");
//        }
//        if (bytes == null) {
//            Log.w(AnkiDroidApp.TAG, "File " + path + " appears to be empty");
//            return "";
//        }
//        MessageDigest md = null;
//        byte[] digest = null;
//        try {
//            md = MessageDigest.getInstance("MD5");
//            digest = md.digest(bytes);
//        } catch (NoSuchAlgorithmException e) {
//            Log.e(AnkiDroidApp.TAG, "Utils.checksum: No such algorithm. " + e.getMessage());
//            throw new RuntimeException(e);
//        }
//        BigInteger biginteger = new BigInteger(1, digest);
//        String result = biginteger.toString(16);
//        // pad with zeros to length of 32
//        if (result.length() < 32) {
//            result = "00000000000000000000000000000000".substring(0, 32 - result.length()) + result;
//        }
//        return result;
//    }
/**
     *  Tempo files
     * ***********************************************************************************************
     */
// tmpdir
// tmpfile
// namedtmp
/**
     * Converts an InputStream to a String.
     * @param is InputStream to convert
     * @return String version of the InputStream
     */
public static String convertStreamToString(InputStream is) {
    String contentOfMyInputStream = "";
    try {
        BufferedReader rd = new BufferedReader(new InputStreamReader(is), 4096);
        String line;
        StringBuilder sb = new StringBuilder();
        while ((line = rd.readLine()) != null) {
            sb.append(line);
        }
        rd.close();
        contentOfMyInputStream = sb.toString();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return contentOfMyInputStream;
}
Also used : InputStreamReader(java.io.InputStreamReader) BufferedReader(java.io.BufferedReader) JSONException(org.json.JSONException) FileNotFoundException(java.io.FileNotFoundException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) IOException(java.io.IOException)

Example 47 with NoSuchAlgorithmException

use of java.security.NoSuchAlgorithmException in project Anki-Android by Ramblurr.

the class Utils method checksum.

/**
     * Checksums
     * ***********************************************************************************************
     */
/**
     * SHA1 checksum.
     * Equivalent to python sha1.hexdigest()
     *
     * @param data the string to generate hash from
     * @return A string of length 40 containing the hexadecimal representation of the MD5 checksum of data.
     */
public static String checksum(String data) {
    String result = "";
    if (data != null) {
        MessageDigest md = null;
        byte[] digest = null;
        try {
            md = MessageDigest.getInstance("SHA1");
            digest = md.digest(data.getBytes("UTF-8"));
        } catch (NoSuchAlgorithmException e) {
            Log.e(AnkiDroidApp.TAG, "Utils.checksum: No such algorithm. " + e.getMessage());
            throw new RuntimeException(e);
        } catch (UnsupportedEncodingException e) {
            Log.e(AnkiDroidApp.TAG, "Utils.checksum: " + e.getMessage());
            e.printStackTrace();
        }
        BigInteger biginteger = new BigInteger(1, digest);
        result = biginteger.toString(16);
        // not 32.
        if (result.length() < 40) {
            String zeroes = "0000000000000000000000000000000000000000";
            result = zeroes.substring(0, zeroes.length() - result.length()) + result;
        }
    }
    return result;
}
Also used : UnsupportedEncodingException(java.io.UnsupportedEncodingException) BigInteger(java.math.BigInteger) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) MessageDigest(java.security.MessageDigest)

Example 48 with NoSuchAlgorithmException

use of java.security.NoSuchAlgorithmException in project StickerCamera by Skykai521.

the class MD5Util method getMD5.

/**
     * MD5加密
     * 
     * @param val
     * @return
     * @throws NoSuchAlgorithmException
     */
public static String getMD5(String val) {
    MessageDigest md5;
    try {
        md5 = MessageDigest.getInstance("MD5");
        md5.update(val.getBytes());
        // 加密
        byte[] m = md5.digest();
        return getString(m);
    } catch (NoSuchAlgorithmException e) {
        return val;
    }
}
Also used : NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) MessageDigest(java.security.MessageDigest)

Example 49 with NoSuchAlgorithmException

use of java.security.NoSuchAlgorithmException in project SeriesGuide by UweTrottmann.

the class Security method verify.

/**
     * Verifies that the signature from the server matches the computed
     * signature on the data.  Returns true if the data is correctly signed.
     *
     * @param publicKey public key associated with the developer account
     * @param signedData signed data from server
     * @param signature server signature
     * @return true if the data and signature match
     */
public static boolean verify(PublicKey publicKey, String signedData, String signature) {
    Signature sig;
    try {
        sig = Signature.getInstance(SIGNATURE_ALGORITHM);
        sig.initVerify(publicKey);
        sig.update(signedData.getBytes());
        if (!sig.verify(Base64.decode(signature))) {
            Timber.e("Signature verification failed.");
            return false;
        }
        return true;
    } catch (NoSuchAlgorithmException | InvalidKeyException | SignatureException | Base64DecoderException e) {
        Timber.e(e, "Signature verification aborted.");
    }
    return false;
}
Also used : Signature(java.security.Signature) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) SignatureException(java.security.SignatureException) InvalidKeyException(java.security.InvalidKeyException)

Example 50 with NoSuchAlgorithmException

use of java.security.NoSuchAlgorithmException in project Signal-Android by WhisperSystems.

the class ConversationActivity method handleInviteLink.

private void handleInviteLink() {
    try {
        String inviteText;
        boolean a = SecureRandom.getInstance("SHA1PRNG").nextBoolean();
        if (a)
            inviteText = getString(R.string.ConversationActivity_lets_switch_to_signal, "https://sgnl.link/1LoIMUl");
        else
            inviteText = getString(R.string.ConversationActivity_lets_use_this_to_chat, "https://sgnl.link/1MF56H1");
        if (isDefaultSms) {
            composeText.appendInvite(inviteText);
        } else {
            Intent intent = new Intent(Intent.ACTION_SENDTO);
            intent.setData(Uri.parse("smsto:" + recipients.getPrimaryRecipient().getNumber()));
            intent.putExtra("sms_body", inviteText);
            intent.putExtra(Intent.EXTRA_TEXT, inviteText);
            startActivity(intent);
        }
    } catch (NoSuchAlgorithmException e) {
        throw new AssertionError(e);
    }
}
Also used : Intent(android.content.Intent) ByteString(com.google.protobuf.ByteString) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException)

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