Java Code Examples for java.util.prefs.Preferences#getByteArray()

The following examples show how to use java.util.prefs.Preferences#getByteArray() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: FallbackProvider.java    From netbeans with Apache License 2.0 6 votes vote down vote up
public Void call() throws Exception { // encryption changing
    LOG.fine("encryption changing");
    Map<String,char[]> saved = new HashMap<String,char[]>();
    Preferences prefs = prefs();
    for (String k : prefs.keys()) {
        if (k.endsWith(DESCRIPTION)) {
            continue;
        }
        byte[] ciphertext = prefs.getByteArray(k, null);
        if (ciphertext == null) {
            continue;
        }
        saved.put(k, encryption.decrypt(ciphertext));
    }
    LOG.log(Level.FINE, "reencrypting keys: {0}", saved.keySet());
    encryption.encryptionChanged();
    for (Map.Entry<String,char[]> entry : saved.entrySet()) {
        prefs.putByteArray(entry.getKey(), encryption.encrypt(entry.getValue()));
    }
    LOG.fine("encryption changing finished");
    return null;
}
 
Example 2
Source File: FallbackProvider.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private boolean testSampleKey(Preferences prefs) {
    byte[] ciphertext = prefs.getByteArray(SAMPLE_KEY, null);
    if (ciphertext == null) {
        encryption.freshKeyring(true);
        byte[] randomArray = new byte[36];
        new SecureRandom().nextBytes(randomArray);
        if (_save(SAMPLE_KEY, (SAMPLE_KEY + new String(randomArray)).toCharArray(),
                NbBundle.getMessage(FallbackProvider.class, "FallbackProvider.sample_key.description"))) {
            LOG.fine("saved sample key");
            return true;
        } else {
            LOG.fine("could not save sample key");
            return false;
        }
    } else {
        encryption.freshKeyring(false);
        while (true) {
            try {
                if (new String(encryption.decrypt(ciphertext)).startsWith(SAMPLE_KEY)) {
                    LOG.fine("succeeded in decrypting sample key");
                    return true;
                } else {
                    LOG.fine("wrong result decrypting sample key");
                }
            } catch (Exception x) {
                LOG.log(Level.FINE, "failed to decrypt sample key", x);
            }
            if (!encryption.decryptionFailed()) {
                LOG.fine("sample key decryption failed");
                return promptToDelete(prefs);
            }
            LOG.fine("will retry decryption of sample key");
        }
    }
}
 
Example 3
Source File: MasterPasswordEncryption.java    From netbeans with Apache License 2.0 5 votes vote down vote up
public @Override boolean enabled() {
    if (Boolean.getBoolean("netbeans.keyring.no.master")) {
        LOG.fine("master password encryption disabled");
        return false;
    }
    if (GraphicsEnvironment.isHeadless()) {
        LOG.fine("disabling master password encryption in headless mode");
        return false;
    }
    try {
        KEY_FACTORY = SecretKeyFactory.getInstance(ENCRYPTION_ALGORITHM);
        encrypt = Cipher.getInstance(ENCRYPTION_ALGORITHM);
        decrypt = Cipher.getInstance(ENCRYPTION_ALGORITHM);
        Preferences prefs = NbPreferences.forModule(Keyring.class);
        Utils.goMinusR(prefs);
        String saltKey = "salt"; // NOI18N
        byte[] salt = prefs.getByteArray(saltKey, null);
        if (salt == null) {
            salt = new byte[36];
            new SecureRandom().nextBytes(salt);
            prefs.putByteArray(saltKey, salt);
        }
        PARAM_SPEC = new PBEParameterSpec(salt, 20);
        LOG.warning("Falling back to master password encryption; " +
                "add -J-Dorg.netbeans.modules.keyring.level=0 to netbeans.conf to see why native keyrings could not be loaded");
        return true;
    } catch (Exception x) {
        LOG.log(Level.INFO, "Cannot initialize security using " + ENCRYPTION_ALGORITHM, x);
        return false;
    }
}
 
Example 4
Source File: ProjectTrust.java    From netbeans with Apache License 2.0 5 votes vote down vote up
ProjectTrust(Preferences prefs) {
    byte[] buf = prefs.getByteArray(KEY_SALT, null);
    if (buf == null) {
        buf = new byte[16];
        new Random().nextBytes(buf);
        prefs.putByteArray(KEY_SALT, buf);
    }
    salt = buf;
    projectTrust = prefs.node(NODE_PROJECT);
    key = new SecretKeySpec(salt, HMAC_SHA256);
}
 
Example 5
Source File: OpenProjectListSettings.java    From netbeans with Apache License 2.0 5 votes vote down vote up
protected final List<ExtIcon> getIconList(String key, boolean allowGrouped) {
    Preferences pref = getPreferences(allowGrouped);
    int count = 0;
    byte[] val = pref.getByteArray(key + "." + count, null);
    List<ExtIcon> toRet = new ArrayList<ExtIcon>();
    while (val != null) {
        toRet.add(val.length > 0 ? new ExtIcon(val) : new ExtIcon());
        count = count + 1;
        val = pref.getByteArray(key + "." + count, null);
    }
    return toRet;
}
 
Example 6
Source File: DefaultCredentialManager.java    From triplea with GNU General Public License v3.0 5 votes vote down vote up
private static @Nullable char[] loadMasterPassword(final Preferences preferences) {
  final byte[] encodedMasterPassword =
      preferences.getByteArray(PREFERENCE_KEY_MASTER_PASSWORD, null);
  if (encodedMasterPassword == null) {
    return null;
  }

  final char[] masterPassword = decodeBytesToChars(encodedMasterPassword);
  scrub(encodedMasterPassword);
  return masterPassword;
}
 
Example 7
Source File: PreferencesHandler.java    From hortonmachine with GNU General Public License v3.0 5 votes vote down vote up
public static byte[] getPreference( String preferenceKey, byte[] defaultValue ) {
    if (preferencesDb != null) {
        return preferencesDb.getPreference(preferenceKey, defaultValue);
    }
    Preferences preferences = Preferences.userRoot().node(PREFS_NODE_NAME);
    byte[] preference = preferences.getByteArray(preferenceKey, defaultValue);
    return preference;
}
 
Example 8
Source File: HuffmanTree.java    From elexis-3-core with Eclipse Public License 1.0 3 votes vote down vote up
/**
 * Import a compaced frequency table from the system preferences (In windows from the registry
 * HKLM/JavaSoft/Prefs/ch/rgw/tools/Compress/StandardTables, in Linux from ~/.prefs)
 * 
 * @param name
 *            Name of the table in the registry. If no table with this name is found, the
 *            default table (TextDeutsch) is returned.
 * @return the table
 */
public static int[] useStandardTable(String name){
	Preferences pr = Preferences.userNodeForPackage(Huff.class);
	Preferences node = pr.node("StandardTables");
	byte[] res = node.getByteArray(name, TextDeutsch);
	return HuffmanTree.expandTable(res);
}