浏览代码

Merge branch 'issue-22' of isundil/pass into master

isundil 7 年之前
父节点
当前提交
95b8beec57

+ 14 - 1
app/src/main/java/info/knacki/pass/io/GPGUtil.java

@@ -313,7 +313,7 @@ public class GPGUtil {
                                         keyringCollection.add(DoChangePassword(secretKeyring, sKey.fPass, result));
                                         ByteArrayOutputStream stream = new ByteArrayOutputStream();
                                         new PGPSecretKeyRingCollection(keyringCollection).encode(stream);
-                                        SettingsManager.SetGPGKeyContent(ctx, SettingsManager.GPG_GENERATED, new ByteArrayInputStream(stream.toByteArray()));
+                                        SettingsManager.SetGPGKeyContent(ctx, new ByteArrayInputStream(stream.toByteArray()));
                                         onDone.OnResponse(null);
                                         return true;
                                     } catch (PGPException | IOException e) {
@@ -336,6 +336,19 @@ public class GPGUtil {
                 });
     }
 
+    public static String GetGPGKeyName(Context ctx) {
+        try {
+            PGPSecretKey key = findSecretKey(getKeyInputStream(ctx));
+            Iterator<String> users = key.getUserIDs();
+            if (users.hasNext())
+                return users.next();
+            return "" +key.getKeyID();
+        } catch (IOException | NoSuchElementException e) {
+            log.log(Level.WARNING, e.getMessage(), e);
+        }
+        return "";
+    }
+
     public static void Generate(final OutputStream out) {
         // FIXME
     }

+ 5 - 13
app/src/main/java/info/knacki/pass/settings/SettingsManager.java

@@ -16,7 +16,6 @@ import java.util.logging.Logger;
 
 public class SettingsManager {
     private static final Logger log = Logger.getLogger(SettingsManager.class.getName());
-    public static final String GPG_GENERATED = "BouncyCastle gpg key";
 
     public static class EncryptionType {
         public static final EncryptionType TYPE_RAW = new EncryptionType(0);
@@ -222,15 +221,15 @@ public class SettingsManager {
             prefs.remove(VCS.class.getSimpleName()).commit();
     }
 
-    private static void doSetGpgKeyContent(Context ctx, String filename, String content) {
-        GetPrefManager(ctx).edit().putString("GPGKeyFile", filename + '@' + content).commit();
+    private static void doSetGpgKeyContent(Context ctx, String content) {
+        GetPrefManager(ctx).edit().putString("GPGKeyFile", content).commit();
     }
 
     public static void RemoveGpgKey(Context ctx) {
         GetPrefManager(ctx).edit().remove("GPGKeyFile").commit();
     }
 
-    public static boolean SetGPGKeyContent(Context ctx, String filename, InputStream stream) {
+    public static boolean SetGPGKeyContent(Context ctx, InputStream stream) {
         if (stream == null) {
             GetPrefManager(ctx).edit().remove("GPGKeyFile").commit();
             return false;
@@ -244,7 +243,7 @@ public class SettingsManager {
                 len = stream.read(buffer);
                 bytes.write(buffer, 0, len);
             } while (len == bufLen);
-            doSetGpgKeyContent(ctx, filename, Base64.encodeToString(bytes.toByteArray(), 0));
+            doSetGpgKeyContent(ctx, Base64.encodeToString(bytes.toByteArray(), 0));
         } catch (IOException e) {
             log.log(Level.SEVERE, "Cannot read file", e);
             RemoveGpgKey(ctx);
@@ -256,14 +255,7 @@ public class SettingsManager {
     public static InputStream GetGPGKeyContent(Context ctx) {
         String content = GetPrefManager(ctx).getString("GPGKeyFile", null);
         if (content != null)
-            return new ByteArrayInputStream(Base64.decode(content.substring(content.indexOf('@') + 1), 0));
-        return null;
-    }
-
-    public static String GetGPGKeyName(Context ctx) {
-        String content = GetPrefManager(ctx).getString("GPGKeyFile", null);
-        if (content != null)
-            return content.substring(0, content.indexOf('@'));
+            return new ByteArrayInputStream(Base64.decode(content, 0));
         return null;
     }
 

+ 2 - 2
app/src/main/java/info/knacki/pass/settings/ui/SettingsActivity.java

@@ -562,7 +562,7 @@ public class SettingsActivity extends AppCompatPreferenceActivity {
                     final Uri intentData = data.getData();
                     boolean gpgImportStatus;
                     if (intentData != null)
-                        gpgImportStatus = SettingsManager.SetGPGKeyContent(getActivity(), GetFileName(intentData), getActivity().getContentResolver().openInputStream(intentData));
+                        gpgImportStatus = SettingsManager.SetGPGKeyContent(getActivity(), getActivity().getContentResolver().openInputStream(intentData));
                     else
                         gpgImportStatus = false;
                     Toast.makeText(getActivity(), getResources().getString(gpgImportStatus ? R.string.gpg_import_ok : R.string.gpg_import_ko), Toast.LENGTH_LONG).show();
@@ -576,7 +576,7 @@ public class SettingsActivity extends AppCompatPreferenceActivity {
 
         protected void updateGpgFileLabel() {
             if (SettingsManager.HasGPGKey(getActivity())) {
-                findPreference(getResources().getString(R.string.id_gpg_keyfile)).setSummary(SettingsManager.GetGPGKeyName(getActivity()));
+                findPreference(getResources().getString(R.string.id_gpg_keyfile)).setSummary(GPGUtil.GetGPGKeyName(getActivity()));
                 findPreference(getResources().getString(R.string.id_gpg_password)).setEnabled(true);
                 findPreference(getResources().getString(R.string.id_gpg_export)).setEnabled(true);
             } else {