Android Open Source - trust Lock Pattern Utils






From Project

Back to project page trust.

License

The source code is released under:

Apache License Version 2.0, January 2004 http://www.apache.org/licenses/ TERMS AND CONDITIONS FOR USE, REPRODUCT...

If you think the Android project trust 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

/*
 * Copyright (C) 2007 The Android Open Source Project
 *//from   w  w w . j  ava 2s .c o  m
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package eu.thedarken.trust.lockpattern;

import java.io.UnsupportedEncodingException;
import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.List;
import java.util.Locale;

public class LockPatternUtils {
    public static final String Utf8 = "utf-8";
    public static final String Sha1 = "sha-1";

    /**
     * Deserialize a pattern.
     *
     * @param string The pattern serialized with {@link #patternToString}
     * @return The pattern.
     */
    public static List<LockPatternView.Cell> stringToPattern(String string) {
        List<LockPatternView.Cell> result = Lists.newArrayList();

        try {
            final byte[] bytes = string.getBytes(Utf8);
            for (int i = 0; i < bytes.length; i++) {
                byte b = bytes[i];
                result.add(LockPatternView.Cell.of(b / 3, b % 3));
            }
        } catch (UnsupportedEncodingException e) {
            // never catch this
        }

        return result;
    }// stringToPattern()

    /**
     * Serialize a pattern.
     *
     * @param pattern The pattern.
     * @return The pattern in string form.
     */
    public static String patternToString(List<LockPatternView.Cell> pattern) {
        if (pattern == null) {
            return "";
        }
        final int patternSize = pattern.size();

        byte[] res = new byte[patternSize];
        for (int i = 0; i < patternSize; i++) {
            LockPatternView.Cell cell = pattern.get(i);
            res[i] = (byte) (cell.getRow() * 3 + cell.getColumn());
        }
        try {
            return new String(res, Utf8);
        } catch (UnsupportedEncodingException e) {
            // never catch this
            return "";
        }
    }// patternToString()

    /**
     * Serializes a pattern
     *
     * @param pattern The pattern
     * @return The SHA-1 string of the pattern got from
     * {@link #patternToString(List)}
     */
    public static String patternToSha1(List<LockPatternView.Cell> pattern) {
        try {
            MessageDigest md = MessageDigest.getInstance(Sha1);
            md.update(patternToString(pattern).getBytes(Utf8));

            byte[] digest = md.digest();
            BigInteger bi = new BigInteger(1, digest);
            return String.format((Locale) null, "%0" + (digest.length * 2)
                    + "x", bi).toLowerCase();
        } catch (NoSuchAlgorithmException e) {
            // never catch this
            return "";
        } catch (UnsupportedEncodingException e) {
            // never catch this
            return "";
        }
    }// patternToSha1()
}




Java Source Code List

eu.thedarken.trust.AppWatcher.java
eu.thedarken.trust.ApplicationTest.java
eu.thedarken.trust.TestGenerator.java
eu.thedarken.trust.TrustActivity.java
eu.thedarken.trust.TrustAutostart.java
eu.thedarken.trust.TrustListAdapter.java
eu.thedarken.trust.TrustListFragment.java
eu.thedarken.trust.TrustPreferences.java
eu.thedarken.trust.TrustReceivers.java
eu.thedarken.trust.TrustService.java
eu.thedarken.trust.db.TrustDB.java
eu.thedarken.trust.dialogs.AboutDialogFragment.java
eu.thedarken.trust.dialogs.DetailsDialogFragment.java
eu.thedarken.trust.dialogs.ExportDialogFragment.java
eu.thedarken.trust.dialogs.FilterDialogFragment.java
eu.thedarken.trust.dialogs.FollowMeDialog.java
eu.thedarken.trust.dialogs.NewsDialogFragment.java
eu.thedarken.trust.lockpattern.Lists.java
eu.thedarken.trust.lockpattern.LockPatternActivity.java
eu.thedarken.trust.lockpattern.LockPatternUtils.java
eu.thedarken.trust.lockpattern.LockPatternView.java
eu.thedarken.trust.logentries.Descriptions.java
eu.thedarken.trust.logentries.IDMaker.java
eu.thedarken.trust.logentries.IntentHelper.java
eu.thedarken.trust.logentries.LogEntryMaker.java
eu.thedarken.trust.logentries.LogEntry.java
eu.thedarken.trust.pro.TrustPro.java