Example usage for org.bouncycastle.crypto.digests TigerDigest TigerDigest

List of usage examples for org.bouncycastle.crypto.digests TigerDigest TigerDigest

Introduction

In this page you can find the example usage for org.bouncycastle.crypto.digests TigerDigest TigerDigest.

Prototype

public TigerDigest() 

Source Link

Document

Standard constructor

Usage

From source file:com.gpfcomics.android.cryptnos.CryptnosApplication.java

License:Open Source License

@Override
public void onCreate() {
    // Do whatever the super needs to do:
    super.onCreate();
    // Open the database using the DB adaptor so we can access the
    // database://from  ww w  . j  av a 2s .co m
    DBHelper = new ParamsDbAdapter(this);
    DBHelper.open();
    // Set the root for all import/export activites.  For now, we'll
    // hard code this to the root directory of the external storage
    // device, usually an SD card.  We may change this in the future.
    importExportRoot = Environment.getExternalStorageDirectory();
    // Get the shared preferences:
    prefs = getSharedPreferences("CryptnosPrefs", Context.MODE_PRIVATE);
    // Set the text encoding.  Ideally, we will get this from the shared
    // preferences, but if it doesn't exist, try to get the default value.
    // We'll try to get the system "file.encoding" value if we can, but
    // fall back on UTF-8 as a last resort.  The try/catch is because
    // System.getProperty() can technically crash on us, but it's probably
    // not very likely.  Note that whatever we get from wherever we get
    // it, we'll write it back to the preferences; this is so the
    // preference gets written at least on the first instance.
    try {
        textEncoding = prefs.getString(PREFS_TEXT_ENCODING,
                System.getProperty("file.encoding", TEXT_ENCODING_UTF8));
    } catch (Exception e) {
        textEncoding = prefs.getString(PREFS_TEXT_ENCODING, TEXT_ENCODING_UTF8);
    }
    SharedPreferences.Editor editor = prefs.edit();
    editor.putString(PREFS_TEXT_ENCODING, textEncoding);
    editor.commit();
    // Generate the parameter salt:
    refreshParameterSalt();
    // Get our boolean preferences: copy to clipboard, show master passwords, and
    // clear passwords on focus loss.
    copyPasswordsToClipboard = prefs.getBoolean(PREFS_COPY_TO_CLIPBOARD, true);
    showMasterPassword = prefs.getBoolean(PREFS_SHOW_MASTER_PASSWD, false);
    clearPasswordsOnFocusLoss = prefs.getBoolean(PREFS_CLEAR_PASSWDS_ON_FOCUS_LOSS, false);
    // Now build our hash table of hash algorithms to lengths:
    try {
        // Get the list of hashes from the string resources:
        String[] hashes = getResources().getStringArray(R.array.hashList);
        // Declare our hash table and initialize its size to the same size
        // as the string array of hash names:
        hashLengths = new Hashtable<String, Integer>(hashes.length);
        // We're using two different hashing engines, the internal one and the
        // Bouncy Castle one, so we'll need a reference to both:
        MessageDigest internalHasher = null;
        Digest bcHasher = null;
        // Declare integers to hold the raw byte length and the Base64-encoded
        // length of each digest:
        int byteLength = 0;
        int b64Length = 0;
        // Loop through the hash name list:
        for (int i = 0; i < hashes.length; i++) {
            // If we're using an internal hasher, get the a copy of the
            // engine and find out its byte length:
            if (hashes[i].compareTo("MD5") == 0 || hashes[i].compareTo("SHA-1") == 0
                    || hashes[i].compareTo("SHA-256") == 0 || hashes[i].compareTo("SHA-384") == 0
                    || hashes[i].compareTo("SHA-512") == 0) {
                internalHasher = MessageDigest.getInstance(hashes[i]);
                byteLength = internalHasher.getDigestLength();
            }
            // For the Bouncy Castle engines, we'll need to declare each
            // individual object and then get its byte length:
            else if (hashes[i].compareTo("RIPEMD-160") == 0) {
                bcHasher = new RIPEMD160Digest();
                byteLength = bcHasher.getDigestSize();
            } else if (hashes[i].compareTo("Tiger") == 0) {
                bcHasher = new TigerDigest();
                byteLength = bcHasher.getDigestSize();
            } else if (hashes[i].compareTo("Whirlpool") == 0) {
                bcHasher = new WhirlpoolDigest();
                byteLength = bcHasher.getDigestSize();
            }
            // Now calculate the Base64-encoded length.  This formula comes
            // from the Base64 Wikipedia article:
            // https://secure.wikimedia.org/wikipedia/en/wiki/Base64
            b64Length = (byteLength + 2 - ((byteLength + 2) % 3)) / 3 * 4;
            // Now store the hash name and length into the hash table:
            hashLengths.put(hashes[i], Integer.valueOf(b64Length));
        }
        // If anything blows up, set the hash table to null, which we'll catch
        // as an error later:
    } catch (Exception e1) {
        hashLengths = null;
    }
}

From source file:com.gpfcomics.android.cryptnos.SiteParameters.java

License:Open Source License

/**
 * Given the user's secret passphrase, combine it with all the other
 * site parameters saved within to produce the generated password and
 * return it to the theApp.//from   w  w  w .j a  va 2s .  c  o  m
 * @param secret The user's secret passphrase, which is never stored.
 * @param handler If not null, this handler will be notified of the
 * progress of the generation process, for the purpose of updating a
 * progress dialog, for example.
 * @return A pseudo-random password generated from the site parameters.
 * @throws Exception Thrown when any error occurs.
 */
public String generatePassword(String secret, Handler handler) throws Exception {
    Message msg = null;
    Bundle b = null;
    ;

    // Asbestos underpants:
    try {
        // The character limit must be zero or greater, while the
        // iteration count must be one or greater:
        if (charLimit >= 0 && iterations > 0) {
            // Concatenate the site and passphrase values, then
            // convert the string to a byte array for hashing:
            byte[] result = site.concat(secret).getBytes(theApp.getTextEncoding());
            // We will use one of two hashing engines.  Internally,
            // Java supports MD5, SHA-1, and a trio of SHA-2 methods.
            // We'll assume that since these are built-in, they must
            // be optimized compared to external definitions.  If
            // the selected hash is one of these, we'll use the
            // internal engine.
            MessageDigest internalHasher = null;
            Digest bcHasher = null;
            if (hash.compareTo("MD5") == 0 || hash.compareTo("SHA-1") == 0 || hash.compareTo("SHA-256") == 0
                    || hash.compareTo("SHA-384") == 0 || hash.compareTo("SHA-512") == 0) {
                internalHasher = MessageDigest.getInstance(hash);
            }
            // If it is any other engine, we'll fall back to the
            // Bouncy Castle implementations.  We may add more later,
            // but for now we'll only use the same ones supported by
            // the .NET version of Cryptnos.  There, we actually
            // re-implemented their code to fit with the .NET hashing
            // class structure; here, we'll using Bouncy Castle out
            // of the box, so it's trivial to implement them all.
            else if (hash.compareTo("RIPEMD-160") == 0) {
                bcHasher = new RIPEMD160Digest();
            } else if (hash.compareTo("Tiger") == 0) {
                bcHasher = new TigerDigest();
            } else if (hash.compareTo("Whirlpool") == 0) {
                bcHasher = new WhirlpoolDigest();
            }
            // If we're using the internal hashing engine, we've
            // got things easy.  The most complex part is feeding the
            // hash back into the engine for multiple iterations.
            if (internalHasher != null) {
                for (int i = 0; i < iterations; i++) {
                    result = internalHasher.digest(result);
                    if (handler != null) {
                        msg = handler.obtainMessage();
                        b = new Bundle();
                        b.putInt("iteration", i);
                        b.putString("password", null);
                        msg.setData(b);
                        handler.sendMessage(msg);

                    }
                }
            }
            // If we're using the Bouncy Castle stuff, we'll need to
            // do a bit more work.  Declare the result, feed it to
            // the engine, and get back the hash.  Note that we redeclare
            // the result byte array after the update so each iteration
            // puts the result hash into the array.  Also note that we
            // make sure to reset the engine after each iteration, which
            // apparently the built-in engines do automagically.
            else if (bcHasher != null) {
                for (int i = 0; i < iterations; i++) {
                    bcHasher.update(result, 0, result.length);
                    result = new byte[bcHasher.getDigestSize()];
                    bcHasher.doFinal(result, 0);
                    bcHasher.reset();
                    if (handler != null) {
                        msg = handler.obtainMessage();
                        b = new Bundle();
                        b.putInt("iteration", i);
                        b.putString("password", null);
                        msg.setData(b);
                        handler.sendMessage(msg);

                    }
                }
            }
            // By now, we *should* have the intermediate hash in hand.
            // We'll double check with a null check here, just in case.
            if (result != null) {
                // Get the raw hash as a Base64 string:
                String b64hash = base64String(result);
                // Now that we've got the hash string, we need to apply
                // our modifications.  First, the character type
                // restriction.  Based on the user's choice in the
                // drop-down, run the hash through some regular
                // expressions to chop out unwanted characters:
                Pattern p = null;
                switch (charTypes) {
                // Alphanumerics, change others to underscores
                case 1:
                    p = Pattern.compile("\\W");
                    b64hash = p.matcher(b64hash).replaceAll("_");
                    break;
                // Alphanumerics only
                case 2:
                    p = Pattern.compile("[^a-zA-Z0-9]");
                    b64hash = p.matcher(b64hash).replaceAll("");
                    break;
                // Alphabetic characters only
                case 3:
                    p = Pattern.compile("[^a-zA-Z]");
                    b64hash = p.matcher(b64hash).replaceAll("");
                    break;
                // Numbers only
                case 4:
                    p = Pattern.compile("\\D");
                    b64hash = p.matcher(b64hash).replaceAll("");
                    break;
                // By default, use all generated characters
                default:
                    break;
                }
                // Next, apply the character limit.  If it's any-
                // thing greater than zero, get only the first so
                // many characters:
                if (charLimit > 0 && b64hash.length() > charLimit)
                    b64hash = b64hash.substring(0, charLimit);
                // Now we have our final value.  Display it back
                // to the user and get ready to save it to the
                // database.
                if (handler != null) {
                    msg = handler.obtainMessage();
                    b = new Bundle();
                    b.putInt("iteration", -100);
                    b.putString("password", b64hash);
                    msg.setData(b);
                    handler.sendMessage(msg);
                }
                return b64hash;
            }
            // If for some reason something failed, the result could
            // be null.  Warn the user as such:
            else {
                throw new Exception(theApp.getResources().getString(R.string.error_null_hash));
            }
        }
        // If the iterations or character limit parsing didn't
        // come up roses, show error messages.  Note that there's
        // also a generic "unknown" one here, just in case, but
        // it's probably irrelevant.
        else {
            if (iterations <= 0)
                throw new Exception(theApp.getResources().getString(R.string.error_bad_iterations));
            else if (charLimit < 0)
                throw new Exception(theApp.getResources().getString(R.string.error_bad_charlimit));
            else
                throw new Exception(theApp.getResources().getString(R.string.error_unknown));
        }
    }
    // This should probably be more robust, but for now just throw back
    // out any exception we caught:
    catch (Exception e) {
        throw e;
    }
}

From source file:edu.vt.middleware.crypt.digest.Tiger.java

License:Open Source License

/** Creates an uninitialized instance of an Tiger digest. */
public Tiger() {
    super(new TigerDigest());
}

From source file:edu.vt.middleware.crypt.digest.Tiger.java

License:Open Source License

/**
 * Creates a new Tiger digest that may optionally be initialized with random
 * data.//from ww w . ja v a  2  s  .  c o m
 *
 * @param  randomize  True to randomize initial state of digest, false
 * otherwise.
 */
public Tiger(final boolean randomize) {
    super(new TigerDigest());
    if (randomize) {
        setRandomProvider(new SecureRandom());
        setSalt(getRandomSalt());
    }
}

From source file:edu.vt.middleware.crypt.digest.Tiger.java

License:Open Source License

/**
 * Creates a new Tiger digest and initializes it with the given salt.
 *
 * @param  salt  Salt data used to initialize digest computation.
 */// w  ww  .j a  va 2s.c o  m
public Tiger(final byte[] salt) {
    super(new TigerDigest());
    setSalt(salt);
}

From source file:org.cryptacular.spec.DigestSpec.java

License:Open Source License

/**
 * Creates a new digest instance./* w  ww.j  a v a 2s. c  o m*/
 *
 * @return  Digest instance.
 */
@Override
public Digest newInstance() {
    final Digest digest;
    if ("GOST3411".equalsIgnoreCase(algorithm)) {
        digest = new GOST3411Digest();
    } else if ("MD2".equalsIgnoreCase(algorithm)) {
        digest = new MD2Digest();
    } else if ("MD4".equalsIgnoreCase(algorithm)) {
        digest = new MD4Digest();
    } else if ("MD5".equalsIgnoreCase(algorithm)) {
        digest = new MD5Digest();
    } else if ("RIPEMD128".equalsIgnoreCase(algorithm) || "RIPEMD-128".equalsIgnoreCase(algorithm)) {
        digest = new RIPEMD128Digest();
    } else if ("RIPEMD160".equalsIgnoreCase(algorithm) || "RIPEMD-160".equalsIgnoreCase(algorithm)) {
        digest = new RIPEMD160Digest();
    } else if ("RIPEMD256".equalsIgnoreCase(algorithm) || "RIPEMD-256".equalsIgnoreCase(algorithm)) {
        digest = new RIPEMD256Digest();
    } else if ("RIPEMD320".equalsIgnoreCase(algorithm) || "RIPEMD-320".equalsIgnoreCase(algorithm)) {
        digest = new RIPEMD320Digest();
    } else if ("SHA1".equalsIgnoreCase(algorithm) || "SHA-1".equalsIgnoreCase(algorithm)) {
        digest = new SHA1Digest();
    } else if ("SHA224".equalsIgnoreCase(algorithm) || "SHA-224".equalsIgnoreCase(algorithm)) {
        digest = new SHA224Digest();
    } else if ("SHA256".equalsIgnoreCase(algorithm) || "SHA-256".equalsIgnoreCase(algorithm)) {
        digest = new SHA256Digest();
    } else if ("SHA384".equalsIgnoreCase(algorithm) || "SHA-384".equalsIgnoreCase(algorithm)) {
        digest = new SHA384Digest();
    } else if ("SHA512".equalsIgnoreCase(algorithm) || "SHA-512".equalsIgnoreCase(algorithm)) {
        digest = new SHA512Digest();
    } else if ("SHA3".equalsIgnoreCase(algorithm) || "SHA-3".equalsIgnoreCase(algorithm)) {
        digest = new SHA3Digest(size);
    } else if ("Tiger".equalsIgnoreCase(algorithm)) {
        digest = new TigerDigest();
    } else if ("Whirlpool".equalsIgnoreCase(algorithm)) {
        digest = new WhirlpoolDigest();
    } else {
        throw new IllegalStateException("Unsupported digest algorithm " + algorithm);
    }
    return digest;
}

From source file:org.jcryptool.visual.hashing.views.HashingView.java

License:Open Source License

private String computeHash(String hashName, String inputText, Text hashText) {
    hash = hash.getName(hashName);/*from ww w.j ava  2s.  com*/
    byte[] digest = null;
    switch (hash) {
    case MD2:
        MD2Digest md2 = new MD2Digest();
        md2.update(inputText.getBytes(), 0, inputText.getBytes().length);
        digest = new byte[md2.getDigestSize()];
        md2.doFinal(digest, 0);

        break;
    case MD4:
        MD4Digest md4 = new MD4Digest();
        md4.update(inputText.getBytes(), 0, inputText.getBytes().length);
        digest = new byte[md4.getDigestSize()];
        md4.doFinal(digest, 0);

        break;
    case MD5:
        MD5Digest md5 = new MD5Digest();
        md5.update(inputText.getBytes(), 0, inputText.getBytes().length);
        digest = new byte[md5.getDigestSize()];
        md5.doFinal(digest, 0);

        break;
    case SHA1:
        SHA1Digest sha1 = new SHA1Digest();
        sha1.update(inputText.getBytes(), 0, inputText.getBytes().length);
        digest = new byte[sha1.getDigestSize()];
        sha1.doFinal(digest, 0);

        break;
    case SHA256:
        SHA256Digest sha256 = new SHA256Digest();
        sha256.update(inputText.getBytes(), 0, inputText.getBytes().length);
        digest = new byte[sha256.getDigestSize()];
        sha256.doFinal(digest, 0);

        break;
    case SHA512:
        SHA512Digest sha512 = new SHA512Digest();
        sha512.update(inputText.getBytes(), 0, inputText.getBytes().length);
        digest = new byte[sha512.getDigestSize()];
        sha512.doFinal(digest, 0);

        break;
    case SHA3_224:
        SHA3.Digest224 sha3_224 = new SHA3.Digest224();
        sha3_224.update(inputText.getBytes(), 0, inputText.getBytes().length);
        digest = new byte[sha3_224.getDigestLength()];
        digest = sha3_224.digest();

        break;
    case SHA3_256:
        SHA3.Digest256 sha3_256 = new SHA3.Digest256();
        sha3_256.update(inputText.getBytes(), 0, inputText.getBytes().length);
        digest = new byte[sha3_256.getDigestLength()];
        digest = sha3_256.digest();

        break;
    case SHA3_384:
        SHA3.Digest384 sha3_384 = new SHA3.Digest384();
        sha3_384.update(inputText.getBytes(), 0, inputText.getBytes().length);
        digest = new byte[sha3_384.getDigestLength()];
        digest = sha3_384.digest();

        break;
    case SHA3_512:
        SHA3.Digest512 sha3_512 = new SHA3.Digest512();
        sha3_512.update(inputText.getBytes(), 0, inputText.getBytes().length);
        digest = new byte[sha3_512.getDigestLength()];
        digest = sha3_512.digest();

        break;
    case SKEIN_256:
        Skein.Digest_256_256 skein_256 = new Skein.Digest_256_256();
        skein_256.update(inputText.getBytes(), 0, inputText.getBytes().length);
        digest = new byte[skein_256.getDigestLength()];
        digest = skein_256.digest();

        break;
    case SKEIN_512:
        Skein.Digest_512_512 skein_512 = new Skein.Digest_512_512();
        skein_512.update(inputText.getBytes(), 0, inputText.getBytes().length);
        digest = new byte[skein_512.getDigestLength()];
        digest = skein_512.digest();

        break;
    case SKEIN_1024:
        Skein.Digest_1024_1024 skein_1024 = new Skein.Digest_1024_1024();
        skein_1024.update(inputText.getBytes(), 0, inputText.getBytes().length);
        digest = new byte[skein_1024.getDigestLength()];
        digest = skein_1024.digest();

        break;
    case RIPEMD160:
        RIPEMD160Digest ripemd160 = new RIPEMD160Digest();
        ripemd160.update(inputText.getBytes(), 0, inputText.getBytes().length);
        digest = new byte[ripemd160.getDigestSize()];
        ripemd160.doFinal(digest, 0);

        break;
    case SM3:
        SM3Digest sm3 = new SM3Digest();
        sm3.update(inputText.getBytes(), 0, inputText.getBytes().length);
        digest = new byte[sm3.getDigestSize()];
        sm3.doFinal(digest, 0);

        break;
    case TIGER:
        TigerDigest tiger = new TigerDigest();
        tiger.update(inputText.getBytes(), 0, inputText.getBytes().length);
        digest = new byte[tiger.getDigestSize()];
        tiger.doFinal(digest, 0);

        break;
    case GOST3411:
        GOST3411Digest gost3411 = new GOST3411Digest();
        gost3411.update(inputText.getBytes(), 0, inputText.getBytes().length);
        digest = new byte[gost3411.getDigestSize()];
        gost3411.doFinal(digest, 0);

        break;
    case WHIRLPOOL:
        WhirlpoolDigest whirlpool = new WhirlpoolDigest();
        whirlpool.update(inputText.getBytes(), 0, inputText.getBytes().length);
        digest = new byte[whirlpool.getDigestSize()];
        whirlpool.doFinal(digest, 0);

        break;
    default:
        break;
    }

    String hashHexValue = new String(Hex.encode(digest));
    if (btnHexadezimal.getSelection()) {
        String hashValueOutput = hashHexValue.toUpperCase().replaceAll(".{2}", "$0 "); //$NON-NLS-1$ //$NON-NLS-2$
        hashText.setText(hashValueOutput);
    } else if (btnDezimal.getSelection()) {
        String hashValue = hexToDecimal(hashHexValue);
        hashValue = hashValue.replaceAll(".{3}", "$0 "); //$NON-NLS-1$ //$NON-NLS-2$
        hashText.setText(hashValue);
    } else if (btnBinary.getSelection()) {
        String hashValue = hexToBinary(hashHexValue);
        hashValue = hashValue.replaceAll(".{8}", "$0#"); //$NON-NLS-1$ //$NON-NLS-2$
        hashText.setText(hashValue);
    }

    return hashHexValue;
}