Search in sources :

Example 21 with DecoderException

use of org.apache.commons.codec.DecoderException in project robovm by robovm.

the class QuotedPrintableCodec method decodeQuotedPrintable.

/**
     * Decodes an array quoted-printable characters into an array of original bytes. Escaped characters are converted
     * back to their original representation.
     * 
     * <p>
     * This function implements a subset of quoted-printable encoding specification (rule #1 and rule #2) as defined in
     * RFC 1521.
     * </p>
     * 
     * @param bytes
     *                  array of quoted-printable characters
     * @return array of original bytes
     * @throws DecoderException
     *                  Thrown if quoted-printable decoding is unsuccessful
     */
public static final byte[] decodeQuotedPrintable(byte[] bytes) throws DecoderException {
    if (bytes == null) {
        return null;
    }
    ByteArrayOutputStream buffer = new ByteArrayOutputStream();
    for (int i = 0; i < bytes.length; i++) {
        int b = bytes[i];
        if (b == ESCAPE_CHAR) {
            try {
                int u = Character.digit((char) bytes[++i], 16);
                int l = Character.digit((char) bytes[++i], 16);
                if (u == -1 || l == -1) {
                    throw new DecoderException("Invalid quoted-printable encoding");
                }
                buffer.write((char) ((u << 4) + l));
            } catch (ArrayIndexOutOfBoundsException e) {
                throw new DecoderException("Invalid quoted-printable encoding");
            }
        } else {
            buffer.write(b);
        }
    }
    return buffer.toByteArray();
}
Also used : DecoderException(org.apache.commons.codec.DecoderException) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Example 22 with DecoderException

use of org.apache.commons.codec.DecoderException in project BIMserver by opensourceBIM.

the class Authorization method fromToken.

public static Authorization fromToken(SecretKeySpec key, String token) throws AuthenticationException {
    if (token == null) {
        throw new IllegalArgumentException("Token required");
    }
    try {
        int hashSizeBytes = 16;
        Cipher decodingCipher = Cipher.getInstance("AES");
        decodingCipher.init(Cipher.DECRYPT_MODE, key);
        ByteBuffer buffer = ByteBuffer.wrap(decodingCipher.doFinal(Hex.decodeHex(token.toCharArray())));
        MessageDigest messageDigest = MessageDigest.getInstance("MD5");
        byte[] foundHash = new byte[hashSizeBytes];
        buffer.get(foundHash, 0, hashSizeBytes);
        byte[] hashInput = new byte[buffer.capacity() - hashSizeBytes];
        buffer.get(hashInput);
        buffer.position(hashSizeBytes);
        byte[] calculatedHash = messageDigest.digest(hashInput);
        if (Arrays.equals(foundHash, calculatedHash)) {
            byte type = buffer.get();
            Authorization authorization = null;
            long expires = buffer.getLong();
            long uoid = buffer.getLong();
            switch(type) {
                case ExplicitRightsAuthorization.ID:
                    authorization = ExplicitRightsAuthorization.fromBuffer(buffer);
                    break;
                case UserAuthorization.ID:
                    authorization = UserAuthorization.fromBuffer(buffer);
                    break;
                case SystemAuthorization.ID:
                    authorization = SystemAuthorization.fromBuffer(buffer);
                    break;
                case AnonymousAuthorization.ID:
                    authorization = AnonymousAuthorization.fromBuffer(buffer);
                    break;
                case AdminAuthorization.ID:
                    authorization = AdminAuthorization.fromBuffer(buffer);
                    break;
                case SingleProjectAuthorization.ID:
                    authorization = SingleProjectAuthorization.fromBuffer(buffer);
                    break;
                case RunServiceAuthorization.ID:
                    authorization = RunServiceAuthorization.fromBuffer(buffer);
                    break;
                default:
                    throw new AuthenticationException("Unknown authorization type: " + type);
            }
            authorization.setUoid(uoid);
            authorization.setExpires(expires);
            if (authorization.getExpires().getTimeInMillis() < new GregorianCalendar().getTimeInMillis()) {
                throw new AuthenticationException("This token has expired");
            }
            return authorization;
        } else {
            throw new AuthenticationException("Given token is corrupt");
        }
    } catch (GeneralSecurityException e) {
        throw new AuthenticationException("Invalid token", e);
    } catch (DecoderException e) {
        throw new AuthenticationException(e);
    }
}
Also used : GeneralSecurityException(java.security.GeneralSecurityException) GregorianCalendar(java.util.GregorianCalendar) ByteBuffer(java.nio.ByteBuffer) DecoderException(org.apache.commons.codec.DecoderException) Cipher(javax.crypto.Cipher) MessageDigest(java.security.MessageDigest)

Example 23 with DecoderException

use of org.apache.commons.codec.DecoderException in project BIMserver by opensourceBIM.

the class TestEnc method start.

private void start() {
    byte[] key = new byte[16];
    new Random().nextBytes(key);
    SecretKeySpec secretKeySpec = new SecretKeySpec(key, "AES");
    String toEncrypt = "1";
    try {
        Cipher encodingCipher = Cipher.getInstance("AES");
        encodingCipher.init(Cipher.ENCRYPT_MODE, secretKeySpec);
        byte[] encodedBytes = encodingCipher.doFinal(toEncrypt.getBytes(Charsets.UTF_8));
        System.out.println("Encoded size: " + encodedBytes.length);
        String encodedHexString = new String(Hex.encodeHex(encodedBytes));
        System.out.println("Encoded hex: " + encodedHexString);
        Cipher decodingCipher = Cipher.getInstance("AES");
        decodingCipher.init(Cipher.DECRYPT_MODE, secretKeySpec);
        ByteBuffer wrap = ByteBuffer.wrap(decodingCipher.doFinal(Hex.decodeHex(encodedHexString.toCharArray())));
        String result = new String(wrap.array(), Charsets.UTF_8);
        System.out.println(result);
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    } catch (NoSuchPaddingException e) {
        e.printStackTrace();
    } catch (InvalidKeyException e) {
        e.printStackTrace();
    } catch (IllegalBlockSizeException e) {
        e.printStackTrace();
    } catch (BadPaddingException e) {
        e.printStackTrace();
    } catch (DecoderException e) {
        e.printStackTrace();
    }
}
Also used : DecoderException(org.apache.commons.codec.DecoderException) Random(java.util.Random) SecretKeySpec(javax.crypto.spec.SecretKeySpec) NoSuchPaddingException(javax.crypto.NoSuchPaddingException) IllegalBlockSizeException(javax.crypto.IllegalBlockSizeException) Cipher(javax.crypto.Cipher) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) BadPaddingException(javax.crypto.BadPaddingException) InvalidKeyException(java.security.InvalidKeyException) ByteBuffer(java.nio.ByteBuffer)

Example 24 with DecoderException

use of org.apache.commons.codec.DecoderException in project Zom-Android by zom.

the class GroupDisplayActivity method inviteContacts.

public void inviteContacts(ArrayList<String> invitees) {
    if (mConn == null)
        return;
    try {
        IChatSessionManager manager = mConn.getChatSessionManager();
        IChatSession session = manager.getChatSession(mAddress);
        for (String invitee : invitees) {
            session.inviteContact(invitee);
            GroupMemberDisplay member = new GroupMemberDisplay();
            XmppAddress address = new XmppAddress(invitee);
            member.username = address.getBareAddress();
            member.nickname = address.getUser();
            try {
                member.avatar = DatabaseUtils.getAvatarFromAddress(getContentResolver(), member.username, ImApp.SMALL_AVATAR_WIDTH, ImApp.SMALL_AVATAR_HEIGHT);
            } catch (DecoderException e) {
                e.printStackTrace();
            }
            mMembers.add(member);
        }
        mRecyclerView.getAdapter().notifyDataSetChanged();
    } catch (Exception e) {
        Log.e(ImApp.LOG_TAG, "error inviting contacts to group", e);
    }
}
Also used : IChatSessionManager(org.awesomeapp.messenger.service.IChatSessionManager) DecoderException(org.apache.commons.codec.DecoderException) XmppAddress(org.awesomeapp.messenger.plugin.xmpp.XmppAddress) IChatSession(org.awesomeapp.messenger.service.IChatSession) DecoderException(org.apache.commons.codec.DecoderException) RemoteException(android.os.RemoteException)

Example 25 with DecoderException

use of org.apache.commons.codec.DecoderException in project Zom-Android by zom.

the class GroupDisplayActivity method updateMembers.

private synchronized void updateMembers() {
    if (mThreadUpdate != null) {
        mThreadUpdate.interrupt();
        mThreadUpdate = null;
    }
    mThreadUpdate = new Thread(new Runnable() {

        @Override
        public void run() {
            final HashMap<String, GroupMemberDisplay> members = new HashMap<>();
            IContactListManager contactManager = null;
            try {
                if (mConn != null) {
                    contactManager = mConn.getContactListManager();
                }
            } catch (RemoteException re) {
            }
            String[] projection = { Imps.GroupMembers.USERNAME, Imps.GroupMembers.NICKNAME, Imps.GroupMembers.ROLE, Imps.GroupMembers.AFFILIATION };
            Uri memberUri = ContentUris.withAppendedId(Imps.GroupMembers.CONTENT_URI, mLastChatId);
            ContentResolver cr = getContentResolver();
            Cursor c = cr.query(memberUri, projection, null, null, null);
            if (c != null) {
                int colUsername = c.getColumnIndex(Imps.GroupMembers.USERNAME);
                int colNickname = c.getColumnIndex(Imps.GroupMembers.NICKNAME);
                int colRole = c.getColumnIndex(Imps.GroupMembers.ROLE);
                int colAffiliation = c.getColumnIndex(Imps.GroupMembers.AFFILIATION);
                while (c.moveToNext()) {
                    GroupMemberDisplay member = new GroupMemberDisplay();
                    member.username = new XmppAddress(c.getString(colUsername)).getBareAddress();
                    member.nickname = c.getString(colNickname);
                    member.role = c.getString(colRole);
                    member.affiliation = c.getString(colAffiliation);
                    try {
                        member.avatar = DatabaseUtils.getAvatarFromAddress(cr, member.username, ImApp.SMALL_AVATAR_WIDTH, ImApp.SMALL_AVATAR_HEIGHT);
                    } catch (DecoderException e) {
                        e.printStackTrace();
                    }
                    if (member.affiliation != null) {
                        if (member.affiliation.contentEquals("owner") || member.affiliation.contentEquals("admin")) {
                            if (member.username.equals(mLocalAddress))
                                mIsOwner = true;
                        }
                    }
                    members.put(member.username, member);
                }
                c.close();
            }
            if (!Thread.currentThread().isInterrupted()) {
                final ArrayList<GroupMemberDisplay> listMembers = new ArrayList<>(members.values());
                // Sort members by name, but keep owners at the top
                Collections.sort(listMembers, new Comparator<GroupMemberDisplay>() {

                    @Override
                    public int compare(GroupMemberDisplay member1, GroupMemberDisplay member2) {
                        if (member1.affiliation == null || member2.affiliation == null)
                            return 1;
                        boolean member1isImportant = (member1.affiliation.contentEquals("owner") || member1.affiliation.contentEquals("admin"));
                        boolean member2isImportant = (member2.affiliation.contentEquals("owner") || member2.affiliation.contentEquals("admin"));
                        if (member1isImportant != member2isImportant) {
                            if (member1isImportant) {
                                return -1;
                            } else {
                                return 1;
                            }
                        }
                        return member1.nickname.compareTo(member2.nickname);
                    }
                });
                runOnUiThread(new Runnable() {

                    @Override
                    public void run() {
                        mMembers = listMembers;
                        if (mRecyclerView != null && mRecyclerView.getAdapter() != null)
                            mRecyclerView.getAdapter().notifyDataSetChanged();
                    }
                });
            }
        }
    });
    mThreadUpdate.start();
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Cursor(android.database.Cursor) Uri(android.net.Uri) ContentResolver(android.content.ContentResolver) DecoderException(org.apache.commons.codec.DecoderException) XmppAddress(org.awesomeapp.messenger.plugin.xmpp.XmppAddress) IContactListManager(org.awesomeapp.messenger.service.IContactListManager) RemoteException(android.os.RemoteException)

Aggregations

DecoderException (org.apache.commons.codec.DecoderException)41 IOException (java.io.IOException)16 CharSequenceX (com.samourai.wallet.util.CharSequenceX)8 MnemonicException (org.bitcoinj.crypto.MnemonicException)8 JSONException (org.json.JSONException)7 DecryptionException (com.samourai.wallet.crypto.DecryptionException)6 ArrayList (java.util.ArrayList)6 Intent (android.content.Intent)5 ProgressDialog (android.app.ProgressDialog)4 HD_Wallet (com.samourai.wallet.hd.HD_Wallet)4 ByteArrayOutputStream (java.io.ByteArrayOutputStream)4 HashMap (java.util.HashMap)4 List (java.util.List)4 BufferedReader (java.io.BufferedReader)3 File (java.io.File)3 FileNotFoundException (java.io.FileNotFoundException)3 ByteBuffer (java.nio.ByteBuffer)3 Map (java.util.Map)3 Cipher (javax.crypto.Cipher)3 AddressFormatException (org.bitcoinj.core.AddressFormatException)3