Search in sources :

Example 41 with Pair

use of net.imglib2.util.Pair in project krypton-android by kryptco.

the class CertifiedPublicKey method parse.

public static CertifiedPublicKey parse(DataInputStream in) throws InvalidPacketTagException, UnsupportedOldPacketLengthTypeException, UnsupportedNewFormatException, UnsupportedPublicKeyAlgorithmException, UnsupportedPublicKeyVersionException, InvalidEd25519PublicKeyFormatException, IOException, InvalidUTF8Exception, DuplicateSubpacketException, NoSuchAlgorithmException, UnsupportedHashAlgorithmException, InvalidSubpacketLengthException, UnsupportedCriticalSubpacketTypeException, UnsupportedSignatureVersionException {
    PublicKeyPacket publicKeyPacket = null;
    boolean lastPacketUserIDOrSignature = false;
    List<Pair<UserIDPacket, List<SignedSignatureAttributes>>> identities = new LinkedList<>();
    while (true) {
        try {
            PacketHeader header = PacketHeader.parse(in);
            Log.d("PGP", "found packet with type " + header.tag.packetType.toString());
            switch(header.tag.packetType) {
                case SIGNATURE:
                    SignedSignatureAttributes signaturePacket = SignedSignatureAttributes.parse(header, in);
                    if (lastPacketUserIDOrSignature && identities.size() > 0) {
                        identities.get(identities.size() - 1).second.add(signaturePacket);
                    }
                    break;
                case PUBLIC_KEY:
                    if (publicKeyPacket != null) {
                        // only accept first public key packet
                        in.skip(header.length.bodyLength);
                        continue;
                    }
                    publicKeyPacket = PublicKeyPacket.parse(header, in);
                    break;
                case USER_ID:
                    identities.add(new Pair<UserIDPacket, List<SignedSignatureAttributes>>(UserIDPacket.parse(header, in), new LinkedList<SignedSignatureAttributes>()));
                    break;
                default:
                    in.skip(header.length.bodyLength);
                    break;
            }
            lastPacketUserIDOrSignature = header.tag.packetType == PacketType.USER_ID || header.tag.packetType == PacketType.SIGNATURE;
        } catch (EOFException e) {
            break;
        }
    }
    return new CertifiedPublicKey(publicKeyPacket, identities);
}
Also used : SignedSignatureAttributes(co.krypt.krypton.pgp.packet.SignedSignatureAttributes) EOFException(java.io.EOFException) PacketHeader(co.krypt.krypton.pgp.packet.PacketHeader) List(java.util.List) LinkedList(java.util.LinkedList) UserIDPacket(co.krypt.krypton.pgp.packet.UserIDPacket) LinkedList(java.util.LinkedList) Pair(android.support.v4.util.Pair)

Example 42 with Pair

use of net.imglib2.util.Pair in project krypton-android by kryptco.

the class AsciiArmor method parse.

public static AsciiArmor parse(String text) throws InvalidAsciiArmorException, UnsupportedHeaderLineException, CryptoException {
    List<String> lines = new LinkedList<>();
    Scanner scanner = new Scanner(text);
    int dataStart = 1;
    boolean foundEmptyLine = false;
    for (int idx = 0; scanner.hasNextLine(); idx++) {
        String line = scanner.nextLine().trim();
        if (line.equals("")) {
            if (foundEmptyLine) {
                break;
            }
            foundEmptyLine = true;
            dataStart = idx + 1;
        }
        lines.add(line);
    }
    scanner.close();
    final int dataEnd = lines.size() - 2;
    if (dataStart > dataEnd) {
        throw new InvalidAsciiArmorException();
    }
    if (lines.size() < 4) {
        throw new InvalidAsciiArmorException("not enough lines");
    }
    String startHeaderLine = lines.get(0).replace(HEADER_LINE_PREFIX, "").replace(HEADER_LINE_SUFFIX, "");
    String endHeaderLine = lines.get(lines.size() - 1).replace(LAST_LINE_PREFIX, "").replace(LAST_LINE_SUFFIX, "");
    if (!startHeaderLine.equals(endHeaderLine)) {
        throw new InvalidAsciiArmorException("start and end header lines do not match");
    }
    HeaderLine headerLine = HeaderLine.fromString(startHeaderLine);
    String b64Data = "";
    for (int i = dataStart; i < dataEnd; i++) {
        b64Data += lines.get(i).trim();
    }
    final byte[] data = Base64.decode(b64Data);
    final int computedCRC = CRC24.compute(data);
    String crcLine = lines.get(lines.size() - 2);
    byte[] crcData = Base64.decode(crcLine.replaceFirst("=", ""));
    if (crcData.length != 3) {
        throw new InvalidAsciiArmorException("crc wrong length");
    }
    ByteBuffer givenCRCBuf = ByteBuffer.allocate(4).put((byte) 0).put(crcData);
    givenCRCBuf.flip();
    int givenCRC = givenCRCBuf.getInt();
    if (givenCRC != computedCRC) {
        throw new InvalidAsciiArmorException("invalid CRC");
    }
    List<Pair<String, String>> headers = new LinkedList<>();
    for (int i = 1; i < dataStart - 1; i++) {
        String header = lines.get(i);
        String[] split = header.split(": ");
        if (split.length != 2) {
            throw new InvalidAsciiArmorException("invalid header");
        }
        headers.add(new Pair<>(split[0], split[1]));
    }
    return new AsciiArmor(headerLine, headers, data);
}
Also used : Scanner(java.util.Scanner) ByteBuffer(java.nio.ByteBuffer) LinkedList(java.util.LinkedList) Pair(android.support.v4.util.Pair)

Example 43 with Pair

use of net.imglib2.util.Pair in project krypton-android by kryptco.

the class GitTest method testCommitHash.

@Test
public void testCommitHash() throws Exception {
    byte[] sigMessage = Base64.decode("iF4EABYKAAYFAlkkmD8ACgkQ4eT0x9ceFp1gNQD+LWiJFax8iQqgr0yJ1P7JFGvMwuZc8r05h6U+X+lyKYEBAK939lEX1rvBmcetftVbRlOMX5oQZwBLt/NJh+nQ3ssC");
    CommitInfo commit = new CommitInfo("2c4df4a89ac5b0b8b21fd2aad4d9b19cd91e7049", "1cd97d0545a25c578e3f4da5283106606276eadf", null, "Alex Grinman <alex@krypt.co> 1495570495 -0400", "Alex Grinman <alex@krypt.co> 1495570495 -0400", "\ntest1234\n".getBytes("UTF-8"));
    AsciiArmor aa = new AsciiArmor(AsciiArmor.HeaderLine.SIGNATURE, Collections.singletonList(new Pair<String, String>("Comment", "Created With Kryptonite")), sigMessage);
    Assert.assertTrue(Arrays.equals(commit.commitHash(aa.toString()), Base16.decode("84e09dac58d81b1f3fc4806b1b4cb18af3cca0ea")));
}
Also used : AsciiArmor(co.krypt.krypton.pgp.asciiarmor.AsciiArmor) CommitInfo(co.krypt.krypton.git.CommitInfo) Pair(android.support.v4.util.Pair) Test(org.junit.Test)

Example 44 with Pair

use of net.imglib2.util.Pair in project LibreraReader by foobnix.

the class ExportSettingsManager method importAll.

public boolean importAll(File file) {
    if (file == null) {
        return false;
    }
    LOG.d("TEST", "Import all from " + file.getPath());
    try {
        String json = new Scanner(file).useDelimiter("\\A").next();
        LOG.d("[IMPORT]", json);
        JSONObject jsonObject = new JSONObject(json);
        importFromJSon(jsonObject.optJSONObject(PREFIX_PDF), pdfSP);
        importFromJSon(jsonObject.optJSONObject(PREFIX_BOOKS), booksSP);
        importFromJSon(jsonObject.optJSONObject(PREFIX_BOOKMARKS_PREFERENCES), viewerSP);
        importFromJSon(jsonObject.optJSONObject(PREFIX_BOOK_CSS), bookCSS);
        jsonToMeta(jsonObject.optJSONArray(PREFIX_RECENT), new ResultResponse<String>() {

            @Override
            public boolean onResultRecive(String result) {
                AppDB.get().addRecent(result);
                return false;
            }
        });
        jsonToMeta(jsonObject.optJSONArray(PREFIX_STARS_Books), new ResultResponse<String>() {

            @Override
            public boolean onResultRecive(String result) {
                AppDB.get().addStarFile(result);
                return false;
            }
        });
        jsonToMeta(jsonObject.optJSONArray(PREFIX_STARS_Folders), new ResultResponse<String>() {

            @Override
            public boolean onResultRecive(String result) {
                AppDB.get().addStarFolder(result);
                return false;
            }
        });
        jsonTagsToMeta(jsonObject.optJSONArray(PREFIX_TAGS_BOOKS), new ResultResponse<Pair<String, String>>() {

            @Override
            public boolean onResultRecive(Pair<String, String> result) {
                try {
                    if (new File(result.first).isFile()) {
                        FileMeta meta = AppDB.get().getOrCreate(result.first);
                        meta.setTag(result.second);
                        AppDB.get().update(meta);
                    }
                } catch (Exception e) {
                    LOG.e(e);
                }
                return false;
            }
        });
        return true;
    } catch (Exception e) {
        LOG.e(e);
        Toast.makeText(c, e.getMessage(), Toast.LENGTH_LONG).show();
    }
    return false;
}
Also used : Scanner(java.util.Scanner) JSONObject(org.json.JSONObject) File(java.io.File) FileMeta(com.foobnix.dao2.FileMeta) JSONException(org.json.JSONException) Pair(android.support.v4.util.Pair)

Example 45 with Pair

use of net.imglib2.util.Pair in project LibreraReader by foobnix.

the class MultyDocSearchDialog method getDialogView.

public static View getDialogView(final FragmentActivity c) {
    View inflate = LayoutInflater.from(c).inflate(R.layout.dialog_multy_search, null, false);
    inflate.setKeepScreenOn(true);
    final EditText editPath = (EditText) inflate.findViewById(R.id.editPath);
    final Button buttonPath = (Button) inflate.findViewById(R.id.buttonPath);
    final EditText editSearchText = (EditText) inflate.findViewById(R.id.editSearchText);
    final Button searchStart = (Button) inflate.findViewById(R.id.searchStart);
    final Button searchStop = (Button) inflate.findViewById(R.id.searchStop);
    infoView1 = (TextView) inflate.findViewById(R.id.infoView1);
    final ListView listView = (ListView) inflate.findViewById(R.id.listView);
    final CheckBox searchInTheSubfolders = (CheckBox) inflate.findViewById(R.id.searchInTheSubfolders);
    final BaseItemLayoutAdapter adapter = new BaseItemLayoutAdapter<Pair<String, Integer>>(c, android.R.layout.simple_list_item_1, Model.get().res) {

        @Override
        public void populateView(View layout, int position, final Pair<String, Integer> item) {
            final File file = new File(item.first);
            TextView t = (TextView) layout.findViewById(android.R.id.text1);
            t.setText(file.getName() + " [" + (item.second == -1 ? "not found" : (item.second + 1)) + "]");
            t.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    ExtUtils.showDocument(c, file, item.second + 1);
                }
            });
        }
    };
    listView.setAdapter(adapter);
    buttonPath.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            ChooserDialogFragment.chooseFolder(c, Model.get().path).setOnSelectListener(new ResultResponse2<String, Dialog>() {

                @Override
                public boolean onResultRecive(String nPath, Dialog dialog) {
                    Model.get().path = nPath;
                    AppState.get().dirLastPath = nPath;
                    editPath.setText(Model.get().path);
                    dialog.dismiss();
                    return false;
                }
            });
        }
    });
    editPath.setText(Model.get().path);
    final Handler updater2 = new Handler() {

        @Override
        public void handleMessage(Message msg) {
            adapter.notifyDataSetChanged();
        }
    };
    final Handler updater1 = new Handler() {

        @Override
        public void handleMessage(Message msg) {
            infoView1.setText((Model.get().currentPage + 1) + "/" + Model.get().currentPagesCount + " " + Model.get().currentDoc);
        }
    };
    searchStart.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            Model.get().text = editSearchText.getText().toString().trim();
            if (TxtUtils.isEmpty(Model.get().text)) {
                Toast.makeText(c, R.string.incorrect_value, Toast.LENGTH_SHORT).show();
                return;
            }
            if (Model.get().text.contains(" ")) {
                Toast.makeText(c, R.string.you_can_search_only_one_word, Toast.LENGTH_SHORT).show();
                return;
            }
            if (Model.get().isSearcingRun) {
                Toast.makeText(c, R.string.searching_please_wait_, Toast.LENGTH_SHORT).show();
                return;
            }
            Model.get().isSearcingRun = true;
            new MyTask(updater1, updater2, c, searchInTheSubfolders.isChecked()).execute();
            infoView1.setText("");
            Model.get().res.clear();
            adapter.notifyDataSetChanged();
            Keyboards.close(editSearchText);
        }
    });
    editSearchText.setOnEditorActionListener(new OnEditorActionListener() {

        @Override
        public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
            boolean handled = false;
            if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                searchStart.performClick();
                handled = true;
            }
            return handled;
        }
    });
    editSearchText.setOnKeyListener(new OnKeyListener() {

        @Override
        public boolean onKey(View v, int keyCode, KeyEvent event) {
            if (keyCode == KeyEvent.KEYCODE_ENTER) {
                searchStart.performClick();
                return true;
            }
            return false;
        }
    });
    searchStop.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            Model.get().isSearcingRun = false;
            infoView1.setText("");
        }
    });
    infoView1.setText("");
    Model.get().res.clear();
    adapter.notifyDataSetChanged();
    return inflate;
}
Also used : EditText(android.widget.EditText) ResultResponse2(com.foobnix.android.utils.ResultResponse2) Message(android.os.Message) Handler(android.os.Handler) View(android.view.View) TextView(android.widget.TextView) ListView(android.widget.ListView) KeyEvent(android.view.KeyEvent) ListView(android.widget.ListView) Button(android.widget.Button) CheckBox(android.widget.CheckBox) Dialog(android.app.Dialog) AlertDialog(android.app.AlertDialog) OnClickListener(android.view.View.OnClickListener) OnKeyListener(android.view.View.OnKeyListener) TextView(android.widget.TextView) OnEditorActionListener(android.widget.TextView.OnEditorActionListener) BaseItemLayoutAdapter(com.foobnix.android.utils.BaseItemLayoutAdapter) File(java.io.File) Pair(android.support.v4.util.Pair)

Aggregations

Pair (android.support.v4.util.Pair)75 ArrayList (java.util.ArrayList)37 View (android.view.View)26 Pair (org.apache.commons.math3.util.Pair)25 ActivityOptionsCompat (android.support.v4.app.ActivityOptionsCompat)16 Intent (android.content.Intent)15 TextView (android.widget.TextView)14 List (java.util.List)12 ImageView (android.widget.ImageView)10 RecyclerView (android.support.v7.widget.RecyclerView)8 AlertDialog (android.support.v7.app.AlertDialog)7 ByteProcessor (ij.process.ByteProcessor)7 HashMap (java.util.HashMap)7 Map (java.util.Map)7 Pair (mpicbg.trakem2.util.Pair)7 NonNull (android.support.annotation.NonNull)6 OsmandSettings (net.osmand.plus.OsmandSettings)6 DialogInterface (android.content.DialogInterface)5 Transition (android.transition.Transition)4 RealLocalizable (net.imglib2.RealLocalizable)4