fork download
  1. package com.incotalk;
  2.  
  3. import com.facebook.crypto.exception.KeyChainException;
  4. import com.facebook.crypto.keychain.KeyChain;
  5. import com.facebook.crypto.keychain.SecureRandomFix;
  6. import java.security.SecureRandom;
  7. import java.util.Arrays;
  8. import android.content.Context;
  9. import android.content.SharedPreferences;
  10. import android.util.Base64;
  11. import android.util.Log;
  12.  
  13. import com.facebook.crypto.cipher.NativeGCMCipher;
  14. import com.facebook.crypto.mac.NativeMac;
  15.  
  16. /**
  17.  * Created by TheKing on 2014-12-29.
  18.  */
  19. public class CustomSharedPrefsBackedKeyChain implements KeyChain {
  20. // Visible for testing.
  21. /* package */ static final String SHARED_PREF_NAME = "crypto";
  22. /* package */ static final String CIPHER_KEY_PREF = "cipher_key";
  23. /* package */ static final String MAC_KEY_PREF = "mac_key";
  24.  
  25. private final SharedPreferences mSharedPreferences;
  26. private final SecureRandom mSecureRandom;
  27. private final String key = "2cda8e8ebb37f2b1";
  28.  
  29. protected byte[] mCipherKey;
  30. protected boolean mSetCipherKey;
  31.  
  32. protected byte[] mMacKey;
  33. protected boolean mSetMacKey;
  34.  
  35. private static final SecureRandomFix sSecureRandomFix = new SecureRandomFix();
  36. private String log = "KeyChain";
  37.  
  38. public CustomSharedPrefsBackedKeyChain(Context context, String key) {
  39. Log.d(log, "CustomSharedPrefsBackedKeyChain");
  40. mSharedPreferences = context.getSharedPreferences(SHARED_PREF_NAME, Context.MODE_PRIVATE);
  41. mSecureRandom = new SecureRandom();
  42. //this.key = key;
  43. }
  44.  
  45. @Override
  46. public synchronized byte[] getCipherKey() throws KeyChainException {
  47. if (!mSetCipherKey) {
  48. mCipherKey = maybeGenerateKey(CIPHER_KEY_PREF, NativeGCMCipher.KEY_LENGTH);
  49. }
  50. mSetCipherKey = true;
  51. return mCipherKey;
  52. }
  53.  
  54. @Override
  55. public byte[] getMacKey() throws KeyChainException {
  56. if (!mSetMacKey) {
  57. mMacKey = maybeGenerateKey(MAC_KEY_PREF, NativeMac.KEY_LENGTH);
  58. }
  59. mSetMacKey = true;
  60. return mMacKey;
  61. }
  62.  
  63. @Override
  64. public byte[] getNewIV() throws KeyChainException {
  65. sSecureRandomFix.tryApplyFixes();
  66. byte[] iv = new byte[NativeGCMCipher.IV_LENGTH];
  67. mSecureRandom.nextBytes(iv);
  68. return iv;
  69. }
  70.  
  71. @Override
  72. public synchronized void destroyKeys() {
  73. mSetCipherKey = false;
  74. mSetMacKey = false;
  75. Arrays.fill(mCipherKey, (byte) 0);
  76. Arrays.fill(mMacKey, (byte) 0);
  77. mCipherKey = null;
  78. mMacKey = null;
  79. SharedPreferences.Editor editor = mSharedPreferences.edit();
  80. editor.remove(CIPHER_KEY_PREF);
  81. editor.remove(MAC_KEY_PREF);
  82. editor.commit();
  83. }
  84.  
  85. /**
  86.   * Generates a key associated with a preference.
  87.   */
  88. private byte[] maybeGenerateKey(String pref, int length) throws KeyChainException {
  89. String base64Key = mSharedPreferences.getString(pref, null);
  90.  
  91. if(length == NativeGCMCipher.KEY_LENGTH)
  92. base64Key = key;
  93. else
  94. base64Key = key + key + key.substring(0, Math.min(key.length(), 16));
  95.  
  96. if (base64Key == null) {
  97. // Generate key if it doesn't exist.
  98. return generateAndSaveKey(pref, length);
  99. } else {
  100. return decodeFromPrefs(base64Key);
  101. }
  102. }
  103.  
  104. private byte[] generateAndSaveKey(String pref, int length) throws KeyChainException {
  105. sSecureRandomFix.tryApplyFixes();
  106. byte[] key = new byte[length];
  107. if(length == NativeGCMCipher.KEY_LENGTH)
  108. key = this.key.getBytes();
  109. else
  110. key = (this.key + this.key + this.key +this.key).getBytes();
  111.  
  112. mSecureRandom.nextBytes(key);
  113. // Store the session key.
  114. SharedPreferences.Editor editor = mSharedPreferences.edit();
  115. editor.putString(
  116. pref,
  117. encodeForPrefs(key));
  118. editor.commit();
  119.  
  120. Log.d(log, key.toString());
  121.  
  122. return key;
  123. }
  124.  
  125. /**
  126.   * Visible for testing.
  127.   */
  128. byte[] decodeFromPrefs(String keyString) {
  129. keyString = this.key;
  130. if (keyString == null) {
  131. return null;
  132. }
  133. Log.d(log, keyString.toString());
  134. return Base64.decode(keyString, Base64.DEFAULT);
  135. }
  136.  
  137. /**
  138.   * Visible for testing.
  139.   */
  140. String encodeForPrefs(byte[] key) {
  141. key = (this.key).getBytes();
  142. if (key == null ) {
  143. return null;
  144. }
  145. return Base64.encodeToString(key, Base64.DEFAULT);
  146. }
  147.  
  148.  
  149. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
Main.java:19: error: class CustomSharedPrefsBackedKeyChain is public, should be declared in a file named CustomSharedPrefsBackedKeyChain.java
public class CustomSharedPrefsBackedKeyChain implements KeyChain {
       ^
Main.java:3: error: package com.facebook.crypto.exception does not exist
import com.facebook.crypto.exception.KeyChainException;
                                    ^
Main.java:4: error: package com.facebook.crypto.keychain does not exist
import com.facebook.crypto.keychain.KeyChain;
                                   ^
Main.java:5: error: package com.facebook.crypto.keychain does not exist
import com.facebook.crypto.keychain.SecureRandomFix;
                                   ^
Main.java:8: error: package android.content does not exist
import android.content.Context;
                      ^
Main.java:9: error: package android.content does not exist
import android.content.SharedPreferences;
                      ^
Main.java:10: error: package android.util does not exist
import android.util.Base64;
                   ^
Main.java:11: error: package android.util does not exist
import android.util.Log;
                   ^
Main.java:13: error: package com.facebook.crypto.cipher does not exist
import com.facebook.crypto.cipher.NativeGCMCipher;
                                 ^
Main.java:14: error: package com.facebook.crypto.mac does not exist
import com.facebook.crypto.mac.NativeMac;
                              ^
Main.java:19: error: cannot find symbol
public class CustomSharedPrefsBackedKeyChain implements KeyChain {
                                                        ^
  symbol: class KeyChain
Main.java:25: error: cannot find symbol
    private final SharedPreferences mSharedPreferences;
                  ^
  symbol:   class SharedPreferences
  location: class CustomSharedPrefsBackedKeyChain
Main.java:35: error: cannot find symbol
    private static final SecureRandomFix sSecureRandomFix = new SecureRandomFix();
                         ^
  symbol:   class SecureRandomFix
  location: class CustomSharedPrefsBackedKeyChain
Main.java:38: error: cannot find symbol
    public CustomSharedPrefsBackedKeyChain(Context context, String key) {
                                           ^
  symbol:   class Context
  location: class CustomSharedPrefsBackedKeyChain
Main.java:46: error: cannot find symbol
    public synchronized byte[] getCipherKey() throws KeyChainException {
                                                     ^
  symbol:   class KeyChainException
  location: class CustomSharedPrefsBackedKeyChain
Main.java:55: error: cannot find symbol
    public byte[] getMacKey() throws KeyChainException {
                                     ^
  symbol:   class KeyChainException
  location: class CustomSharedPrefsBackedKeyChain
Main.java:64: error: cannot find symbol
    public byte[] getNewIV() throws KeyChainException {
                                    ^
  symbol:   class KeyChainException
  location: class CustomSharedPrefsBackedKeyChain
Main.java:88: error: cannot find symbol
    private byte[] maybeGenerateKey(String pref, int length) throws KeyChainException {
                                                                    ^
  symbol:   class KeyChainException
  location: class CustomSharedPrefsBackedKeyChain
Main.java:104: error: cannot find symbol
    private byte[] generateAndSaveKey(String pref, int length) throws KeyChainException {
                                                                      ^
  symbol:   class KeyChainException
  location: class CustomSharedPrefsBackedKeyChain
Main.java:35: error: cannot find symbol
    private static final SecureRandomFix sSecureRandomFix = new SecureRandomFix();
                                                                ^
  symbol:   class SecureRandomFix
  location: class CustomSharedPrefsBackedKeyChain
Main.java:39: error: cannot find symbol
        Log.d(log, "CustomSharedPrefsBackedKeyChain");
        ^
  symbol:   variable Log
  location: class CustomSharedPrefsBackedKeyChain
Main.java:40: error: cannot find symbol
        mSharedPreferences = context.getSharedPreferences(SHARED_PREF_NAME, Context.MODE_PRIVATE);
                                                                            ^
  symbol:   variable Context
  location: class CustomSharedPrefsBackedKeyChain
Main.java:48: error: cannot find symbol
            mCipherKey = maybeGenerateKey(CIPHER_KEY_PREF, NativeGCMCipher.KEY_LENGTH);
                                                           ^
  symbol:   variable NativeGCMCipher
  location: class CustomSharedPrefsBackedKeyChain
Main.java:45: error: method does not override or implement a method from a supertype
    @Override
    ^
Main.java:57: error: cannot find symbol
            mMacKey = maybeGenerateKey(MAC_KEY_PREF, NativeMac.KEY_LENGTH);
                                                     ^
  symbol:   variable NativeMac
  location: class CustomSharedPrefsBackedKeyChain
Main.java:54: error: method does not override or implement a method from a supertype
    @Override
    ^
Main.java:66: error: cannot find symbol
        byte[] iv = new byte[NativeGCMCipher.IV_LENGTH];
                             ^
  symbol:   variable NativeGCMCipher
  location: class CustomSharedPrefsBackedKeyChain
Main.java:63: error: method does not override or implement a method from a supertype
    @Override
    ^
Main.java:79: error: package SharedPreferences does not exist
        SharedPreferences.Editor editor = mSharedPreferences.edit();
                         ^
Main.java:71: error: method does not override or implement a method from a supertype
    @Override
    ^
Main.java:91: error: cannot find symbol
        if(length == NativeGCMCipher.KEY_LENGTH)
                     ^
  symbol:   variable NativeGCMCipher
  location: class CustomSharedPrefsBackedKeyChain
Main.java:107: error: cannot find symbol
        if(length == NativeGCMCipher.KEY_LENGTH)
                     ^
  symbol:   variable NativeGCMCipher
  location: class CustomSharedPrefsBackedKeyChain
Main.java:114: error: package SharedPreferences does not exist
        SharedPreferences.Editor editor = mSharedPreferences.edit();
                         ^
Main.java:120: error: cannot find symbol
        Log.d(log, key.toString());
        ^
  symbol:   variable Log
  location: class CustomSharedPrefsBackedKeyChain
Main.java:133: error: cannot find symbol
        Log.d(log, keyString.toString());
        ^
  symbol:   variable Log
  location: class CustomSharedPrefsBackedKeyChain
Main.java:134: error: cannot find symbol
        return Base64.decode(keyString, Base64.DEFAULT);
                                        ^
  symbol:   variable Base64
  location: class CustomSharedPrefsBackedKeyChain
Main.java:134: error: cannot find symbol
        return Base64.decode(keyString, Base64.DEFAULT);
               ^
  symbol:   variable Base64
  location: class CustomSharedPrefsBackedKeyChain
Main.java:145: error: cannot find symbol
        return Base64.encodeToString(key, Base64.DEFAULT);
                                          ^
  symbol:   variable Base64
  location: class CustomSharedPrefsBackedKeyChain
Main.java:145: error: cannot find symbol
        return Base64.encodeToString(key, Base64.DEFAULT);
               ^
  symbol:   variable Base64
  location: class CustomSharedPrefsBackedKeyChain
39 errors
stdout
Standard output is empty