Java 8 KeyStore - top ranked examples from Open Source projects

These code examples were ranked by Codota’s semantic indexing as the best open source examples for Java 8 KeyStore class.

This code example shows how to use the following methods: load, setCertificateEntry, store
                    mKnownServersStore.load(in, LOCAL_TRUSTSTORE_PASSWORD.toCharArray());
                } finally { 
                    in.close();
                } 
            } else { 
                mKnownServersStore.load(null, LOCAL_TRUSTSTORE_PASSWORD.toCharArray()); // necessary to initialize an empty KeyStore instance
            } 
        } 
        return mKnownServersStore;
    } 
     
     
    public static void addCertToKnownServersStore(Certificate cert, Context context) throws  KeyStoreException, NoSuchAlgorithmException, 
                                                                                            CertificateException, IOException { 
        KeyStore knownServers = getKnownServersStore(context);
        knownServers.setCertificateEntry(Integer.toString(cert.hashCode()), cert);
        FileOutputStream fos = null;
        try { 
            fos = context.openFileOutput(LOCAL_TRUSTSTORE_FILENAME, Context.MODE_PRIVATE);
            knownServers.store(fos, LOCAL_TRUSTSTORE_PASSWORD.toCharArray());

7
CodeRank
Connect your IDE to all the code out there  Get Codota for Java
This code example shows how to use the following methods: load, setCertificateEntry, setKeyEntry, store
        signingCert = CertificateCreator.createTypicalMasterCert(caKeypair);
 
        log.fine("Done generating signing cert");
        log.fine(String.valueOf(signingCert));
 
        _ks.load(null, _keystorepass);
 
        _ks.setCertificateEntry(_caCertAlias, signingCert);
        _ks.setKeyEntry(_caPrivKeyAlias, caPrivKey, _keypassword, new Certificate[] {signingCert});
 
        File caKsFile = new File(root, _caPrivateKeystore);
 
        OutputStream os = new FileOutputStream(caKsFile);
        _ks.store(os, _keystorepass);
 
        log.fine("Wrote JKS keystore to: " +
            caKsFile.getAbsolutePath());
 
        // also export a .cer that can be imported as a trusted root 
        // to disable all warning dialogs for interception 
Full Snippet Info
7
CodeRank
Connect your IDE to all the code out there  Get Codota for Java
This code example shows how to use the following methods: getCertificate, getCertificateAlias, getCertificateChain, setCertificateEntry
 
        if (keyAlias != null)
        { 
            try 
            { 
                validate(keyStore, keyStore.getCertificate(keyAlias));
            } 
            catch (KeyStoreException kse)
            { 
                LOG.debug(kse);
                throw new CertificateException("Unable to validate certificate" +
                        " for alias [" + keyAlias + "]: " + kse.getMessage(), kse);
            } 
            result = keyAlias;            
        } 
         
        return result;
    } 
     
    /** 

7
CodeRank
Connect your IDE to all the code out there  Get Codota for Java
This code example shows how to use the following methods: containsAlias, getKey, load, store
    } 
 
    private void checkStore(KeyStore ks, byte[] data)
        throws Exception 
    { 
        ks.load(new ByteArrayInputStream(data), oldStorePass);
 
        if (!ks.containsAlias("android"))
        { 
            fail("cannot find alias"); 
        } 
 
        Key key = ks.getKey("android", oldStorePass);
        if (key == null)
        { 
            fail("cannot find key"); 
        } 
 
        ByteArrayOutputStream bOut = new ByteArrayOutputStream();
 

7
CodeRank
Connect your IDE to all the code out there  Get Codota for Java
This code example shows how to use the following methods: containsAlias, deleteEntry, setCertificateEntry, store
    } 
 
    private synchronized void store() { 
        try { 
            FileOutputStream fos = mContext.openFileOutput(KEYSTORE_FILENAME, Context.MODE_PRIVATE);
            mKeyStore.store(fos, KEYSTORE_PASSWORD);
            fos.close();
        } catch (IOException e) {
            throw new IllegalStateException("Unable to store keyStore", e);
        } catch (GeneralSecurityException e) {
            throw new IllegalStateException("Unable to store keyStore", e);
        } 
    } 
 
    /** 
     * Returns the name that should be used in a new certificate. 
     * <p> 
     * The format is: "CN=anymote/PRODUCT/DEVICE/MODEL/unique identifier" 
     */ 
    private static final String getCertificateName(String id) {

6
CodeRank
Connect your IDE to all the code out there  Get Codota for Java
This code example shows how to use the following methods: getCertificateChain, getKey, load
		initKeyStore(); 
	} 
 
	private void initKeyStore() throws GeneralSecurityException, IOException {
		if(!storeFile.exists() || !loadFromFile()) {
			keyStore.load(null, password);
		} 
	} 
 
	private boolean loadFromFile() throws GeneralSecurityException, IOException {
		InputStream input = null;
		try { 
			input = new FileInputStream(storeFile);
			keyStore.load(input, password);
			caPrivateKey = (PrivateKey) keyStore.getKey(STORE_KEY, password);
			final Certificate[] chain = keyStore.getCertificateChain(STORE_KEY);
			caCertificate = (X509Certificate) chain[0];
			return true; 
		} catch (FileNotFoundException e) {
			return false; 
		} finally { 
Full Snippet Info
6
CodeRank
Connect your IDE to all the code out there  Get Codota for Java
This code example shows how to use the following methods: store
  private static void saveKeyStore(KeyStore ks, String filename,
                                   String password)
    throws GeneralSecurityException, IOException { 
    FileOutputStream out = new FileOutputStream(filename);
    try { 
      ks.store(out, password.toCharArray());
    } finally { 
      out.close();
    } 
  } 
 
  public static void createKeyStore(String filename,
                                    String password, String alias,
                                    Key privateKey, Certificate cert)
    throws GeneralSecurityException, IOException { 
    KeyStore ks = createEmptyKeyStore();
    ks.setKeyEntry(alias, privateKey, password.toCharArray(),
                   new Certificate[]{cert});
    saveKeyStore(ks, filename, password);
  } 
Full Snippet Info
6
CodeRank
Connect your IDE to all the code out there  Get Codota for Java
This code example shows how to use the following methods: getCertificateChain, getKey, load
			logger.info("No keystore provided, keys and certificates will be transient!");
		} 
		if (file.exists()) {
			logger.fine("Loading keys from " + filename);
			InputStream is = new FileInputStream(file);
			keystore.load(is, password);
			caKey = (PrivateKey) keystore.getKey(CA, password);
			if (caKey == null) {
				logger.warning("Keystore does not contain an entry for '" + CA
						+ "'"); 
			} 
			caCerts = cast(keystore.getCertificateChain(CA));
			initSerials(); 
		} else { 
			logger.info("Generating CA key");
			keystore.load(null, password);
			generateCA(caName);
		} 
	} 
 
	/** 
Full Snippet Info
6
CodeRank
Connect your IDE to all the code out there  Get Codota for Java
This code example shows how to use the following methods: aliases, getType, load, size
 
	public SecureStorageFor43() throws DigSigException { 
 
		try { 
			secureStorage = KeyStore.getInstance("AndroidKeyStore");
			secureStorage.load(null);
			 
			Log.i(TAG, "Key store initialized. Type: " + secureStorage.getType() + "");
			Log.d(TAG, "Key store size: " + secureStorage.size());
			Log.d(TAG, "Key store alises: " + secureStorage.aliases());
		} catch (Exception e) {
			Log.e(TAG, "Failed to initialize", e);
			throw new DigSigException(e);
		} 
	} 
 
	@Override 
	public X509Certificate getCertificate(int index) {
		 
		String certKey = Keywords.certificate(index);
Full Snippet Info
6
CodeRank
Connect your IDE to all the code out there  Get Codota for Java
This code example shows how to use the following methods: getCertificate, setCertificateEntry
		String keyfile = (String) args.get("-file");
		if (alias.length() < 1) {
			throw new IllegalArgumentException("no alias given");
		} 
 
		Certificate c = ks.getCertificate(alias);
		if (c != null) {
			System.out.print("alias already in keystore: ");
			print(c);
			return; 
		} 
 
		FileInputStream fis = new FileInputStream(keyfile);
		X509Certificate cert = new X509CertImpl(fis);
		fis.close();
 
		System.out.print("imported '" + alias + "' ");
		print(cert);
 
		ks.setCertificateEntry(alias, cert);

6
CodeRank
Connect your IDE to all the code out there  Get Codota for Java

Related Java 8 KeyStore Questions & Answers:

See Code Examples for Java 8 KeyStore Methods: