Android Open Source - PasswordDroid Entry Factory






From Project

Back to project page PasswordDroid.

License

The source code is released under:

GNU General Public License

If you think the Android project PasswordDroid listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package de.wuthoehle.passworddroid.service.model.entries;
/*from w  w  w  .  ja  v a  2 s.co m*/
/* Copyright (c) 2015 Marco Huenseler <marcoh.huenseler+git@gmail.com>
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

import java.util.ArrayList;
import java.util.List;
import java.util.Queue;

import de.wuthoehle.passworddroid.service.model.Category;
import de.wuthoehle.passworddroid.service.model.FileFormat;

public class EntryFactory {

    private Queue<Byte> bytestream;
    private String name, user, comment, add_characters;
    private List<String> salts;
    private int counter, length;
    private Category category;
    private byte mask;

    public EntryFactory(Queue<Byte> bytestream, Category parent) {
        this.bytestream = bytestream;
        this.category = parent;
        resetFields();
    }

    public Entry getEntry() {
        // Check if we have a DerivatorEntry here
        if (this.name != null
                && this.user != null
                && this.comment != null
                && this.add_characters != null
                && this.salts != null
                && this.counter >= 0
                && this.length >= 0
                && this.mask != (byte) 0xFF) {
            return new DerivatorEntry(this.name, this.user, this.category,
                    this.counter, this.mask, this.add_characters, this.length,
                    this.comment, this.salts);
        }

        Entry result = new Entry(this.name, this.user, this.category);
        List<EntryElement> elements = new ArrayList<>();

        // Countable
        if (this.counter >= 0) {
            elements.add(new Countable(result, this.counter));
        }
        // Customizable
        if (this.mask != (byte) 0xFF && this.add_characters != null && this.length >= 0) {
            elements.add(new Customizable(result, this.mask, this.add_characters, this.length));
        }
        // Commentable
        if (this.comment != null) {
            elements.add(new Commentable(result, this.comment));
        }
        // Saltable
        if (this.salts != null) {
            elements.add(new Saltable(result, this.salts));
        }

        result.setElements(elements);
        return result;
    }

    public void parseEntry() throws FileFormat.MalformedDatabaseFileException {
        // Entry:
        // 1 Byte ID | 4 Bytes size-> | x Bytes UTF-8 Service Name | 4 Bytes size-> | x Bytes UTF-8 User Name | 4 Bytes element count
        resetFields();

        byte id = FileFormat.getByte(bytestream);
        if (id != FileFormat.ID_ENTRY) {
            throw new FileFormat.MalformedDatabaseFileException("Entry ID not found!");
        }

        int service_size = FileFormat.getInteger(bytestream);
        this.name = FileFormat.getString(bytestream, service_size);

        int user_size = FileFormat.getInteger(bytestream);
        this.user = FileFormat.getString(bytestream, user_size);

        int elements_size = FileFormat.getInteger(bytestream);
        for (int i = 0; i < elements_size; i++) {
            int next_type = FileFormat.getByte(bytestream);
            switch (next_type) {
                case FileFormat.ID_COUNTABLE:
                    parseCountable();
                    break;
                case FileFormat.ID_CUSTOMIZABLE:
                    parseCustomizable();
                    break;
                case FileFormat.ID_COMMENTABLE:
                    parseCommentable();
                    break;
                case FileFormat.ID_SALTABLE:
                    parseSaltable();
                    break;
                default:
                    throw new FileFormat.MalformedDatabaseFileException("Next element type unknown!");
                    // auto-break ;)
            }
        }
    }

    private void parseCommentable() throws FileFormat.MalformedDatabaseFileException {
        // Commentable:
        // (1 Byte ID |) 4 Bytes size-> | x Bytes UTF-8 Comment

        int comment_size = FileFormat.getInteger(bytestream);
        this.comment = FileFormat.getString(bytestream, comment_size);
    }

    private void parseCountable() throws FileFormat.MalformedDatabaseFileException {
        // Countable:
        // (1 Byte ID |) 4 Bytes counter

        this.counter = FileFormat.getInteger(bytestream);
    }

    private void parseCustomizable() throws FileFormat.MalformedDatabaseFileException {
        // Customizable:
        // (1 Byte ID |) 1 Byte charmask | 4 Bytes size-> | x Bytes UTF-8 additional characters | 4 Bytes Length
        this.mask = FileFormat.getByte(bytestream);
        int add_characters_size = FileFormat.getInteger(bytestream);
        this.add_characters = FileFormat.getString(bytestream, add_characters_size);
        this.length = FileFormat.getInteger(bytestream);
    }

    private void parseSaltable() throws FileFormat.MalformedDatabaseFileException {
        // Saltable:
        // (1 Byte ID |) 4 Byte salts count-> | x*[4 Bytes size-> | y Bytes UTF-8 Salt]

        int salts_count = FileFormat.getInteger(bytestream);
        this.salts = new ArrayList<>(salts_count);
        for (int i = 0; i < salts_count; i++) {
            int size = FileFormat.getInteger(bytestream);
            this.salts.add(FileFormat.getString(bytestream, size));
        }

    }

    private void resetFields() {
        this.name = null;
        this.user = null;
        this.comment = null;
        this.add_characters = null;
        this.salts = null;
        this.counter = -1;
        this.length = -1;
        this.mask = (byte) 0xFF;
    }

}




Java Source Code List

com.lambdaworks.crypto.PBKDF.java
com.lambdaworks.crypto.SCrypt.java
de.wuthoehle.passworddroid.ApplicationTest.java
de.wuthoehle.passworddroid.PasswordDerivateActivity.java
de.wuthoehle.passworddroid.crypto.AESprng.java
de.wuthoehle.passworddroid.crypto.DerivationRunnableFactory.java
de.wuthoehle.passworddroid.crypto.Util.java
de.wuthoehle.passworddroid.crypto.SCryptParameters.SCryptParametersFactory.java
de.wuthoehle.passworddroid.crypto.SCryptParameters.SCryptParameters.java
de.wuthoehle.passworddroid.service.PasswordDroidService.java
de.wuthoehle.passworddroid.service.model.Category.java
de.wuthoehle.passworddroid.service.model.Container.java
de.wuthoehle.passworddroid.service.model.Database.java
de.wuthoehle.passworddroid.service.model.DerivatorCategory.java
de.wuthoehle.passworddroid.service.model.DerivatorDatabase.java
de.wuthoehle.passworddroid.service.model.FileFormat.java
de.wuthoehle.passworddroid.service.model.entries.Commentable.java
de.wuthoehle.passworddroid.service.model.entries.Countable.java
de.wuthoehle.passworddroid.service.model.entries.Customizable.java
de.wuthoehle.passworddroid.service.model.entries.DerivatorEntry.java
de.wuthoehle.passworddroid.service.model.entries.EntryElement.java
de.wuthoehle.passworddroid.service.model.entries.EntryFactory.java
de.wuthoehle.passworddroid.service.model.entries.EntryVisitor.java
de.wuthoehle.passworddroid.service.model.entries.Entry.java
de.wuthoehle.passworddroid.service.model.entries.GetBundleVisitor.java
de.wuthoehle.passworddroid.service.model.entries.SaltVisitor.java
de.wuthoehle.passworddroid.service.model.entries.Saltable.java
de.wuthoehle.passworddroid.service.model.entries.WriteVisitor.java