Java 8 KeyPair - 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 KeyPair class.

This code example shows how to use the following methods:getPrivate, getPublic
    public KeyAgreementHelper(String algorithmName) {
        this.algorithmName = algorithmName;
    } 
 
    @Override public void test(KeyPair keyPair) {
        test(keyPair.getPrivate(), keyPair.getPublic());
    } 
 
    void test(PrivateKey encryptKey, PublicKey decryptKey) {
 
        KeyAgreement keyAgreement = null;
        try { 
            keyAgreement = KeyAgreement.getInstance(algorithmName);
        } catch (NoSuchAlgorithmException e) {
            Assert.fail(e.getMessage());
        } 
 
        try { 
            keyAgreement.init(encryptKey);
        } catch (InvalidKeyException e) {
8
CodeRank
This code example shows how to use the following methods:getPrivate, getPublic
 
  @Test public void multipleHostnamesForOneCertificate() throws Exception {
    CertificatePinner certificatePinner = new CertificatePinner.Builder()
        .add("example.com", keypairACertificate1Pin)
        .add("www.example.com", keypairACertificate1Pin)
        .build(); 
 
    certificatePinner.check("example.com", keypairACertificate1);
    certificatePinner.check("www.example.com", keypairACertificate1);
  } 
 
  @Test public void absentHostnameMatches() throws Exception {
    CertificatePinner certificatePinner = new CertificatePinner.Builder().build();
    certificatePinner.check("example.com", keypairACertificate1);
  } 
} 
7
CodeRank
This code example shows how to use the following methods:getPrivate, getPublic
		result.put("value", verifierKey);
		return result;
	} 
	 
	public void setKeyPair(KeyPair keyPair) {
		PrivateKey privateKey = keyPair.getPrivate();
		Assert.state(privateKey instanceof RSAPrivateKey, "KeyPair must be an RSA ");
		signer = new RsaSigner((RSAPrivateKey) privateKey);
		RSAPublicKey publicKey = (RSAPublicKey) keyPair.getPublic();
		verifier = new RsaVerifier(publicKey);
		verifierKey = "-----BEGIN PUBLIC KEY-----\n" + new String(Base64.encode(publicKey.getEncoded())) + "\n-----END PUBLIC KEY-----";
	} 
 
	/** 
	 * Sets the JWT signing key. It can be either a simple MAC key or an RSA key. RSA keys should be in OpenSSH format, 
	 * as produced by <tt>ssh-keygen</tt>. 
	 *  
	 * @param key the key to be used for signing JWTs. 
	 */ 
	public void setSigningKey(String key) {
7
CodeRank
This code example shows how to use the following methods:getPublic
	private static final Logger log = LoggerFactory.getLogger(PrivateDataHelper.class); 
 
	@Inject 
	ServiceAuthorizationService serviceAuthorizationService;
 
	public static final ServiceMetadataKey METADATA_SSHKEY = new ServiceMetadataKey("sshkey");
 
	public String findData(ProjectId projectId, ServiceType serviceType, ServiceMetadataKey keyId) throws OpsException {
		String keyData;
		try { 
			keyData = serviceAuthorizationService.findPrivateData(serviceType, projectId, keyId);
		} catch (RepositoryException e) {
			throw new OpsException("Error reading from repository", e);
		} 
		return keyData;
	} 
 
	public KeyPair findKeyPair(ProjectId projectId, ServiceType serviceType, ServiceMetadataKey keyId)
			throws OpsException { 
		String keyData = findData(projectId, serviceType, keyId);

7
CodeRank
This code example shows how to use the following methods:getPrivate
        Certificate cert = keystore.getCertificate("bob");
        return cert.getPublicKey();
    } 
 
    public static PrivateKey privateRSAKey() throws Exception {
        return rsaPair.getPrivate();
    } 
 
    public static PublicKey publicRSAKey() throws Exception {
        return rsaPair.getPublic();
    } 
 
    public static SecureRandom random() throws Exception {
        return new SecureRandom();
    } 
 
    @Override 
    protected JndiRegistry createRegistry() throws Exception {
        return super.createRegistry(); 
    } 
7
CodeRank
This code example shows how to use the following methods:getPrivate, getPublic
    public void testReadRsaPassphraseKeyAndWriteWithoutPassphrase() throws Exception { 
        KeyPair key = SecureKeys.readPem(ResourceUtils.create(this).getResourceFromUrl("classpath://brooklyn/util/crypto/sample_rsa_passphrase.pem"), "passphrase");
        checkNonTrivial(key);
        File f = File.createTempFile("brooklyn-sample_rsa_passphrase_without_passphrase", "pem");
        f.deleteOnExit();
        Files.write(SecureKeys.stringPem(key), f, Charset.defaultCharset());
        KeyPair key2 = SecureKeys.readPem(new FileInputStream(f), null);
        checkNonTrivial(key2);
        Assert.assertEquals(key2.getPrivate().getEncoded(), key.getPrivate().getEncoded());
        Assert.assertEquals(key2.getPublic().getEncoded(), key.getPublic().getEncoded());
    } 
 
    private void checkNonTrivial(KeyPair key) {
        Assert.assertNotEquals(key.getPrivate().getEncoded().length, 0);
        Assert.assertNotEquals(key.getPublic().getEncoded().length, 0);
    } 
 
} 
7
CodeRank
This code example shows how to use the following methods:getPrivate, getPublic
   * @throws GeneralSecurityException thrown if an Security error ocurred. 
   */ 
  public static X509Certificate generateCertificate(String dn, KeyPair pair,
                                                    int days, String algorithm)
    throws GeneralSecurityException, IOException { 
    PrivateKey privkey = pair.getPrivate();
    X509CertInfo info = new X509CertInfo();
    Date from = new Date();
    Date to = new Date(from.getTime() + days * 86400000l);
    CertificateValidity interval = new CertificateValidity(from, to);
    BigInteger sn = new BigInteger(64, new SecureRandom());
    X500Name owner = new X500Name(dn);
 
    info.set(X509CertInfo.VALIDITY, interval);
    info.set(X509CertInfo.SERIAL_NUMBER, new CertificateSerialNumber(sn));
    info.set(X509CertInfo.SUBJECT, new CertificateSubjectName(owner));
    info.set(X509CertInfo.ISSUER, new CertificateIssuerName(owner));
    info.set(X509CertInfo.KEY, new CertificateX509Key(pair.getPublic()));
    info
      .set(X509CertInfo.VERSION, new CertificateVersion(CertificateVersion.V3)); 

7
CodeRank
This code example shows how to use the following methods:getPrivate, getPublic
7
CodeRank
This code example shows how to use the following methods:getPrivate, getPublic
   * @throws GeneralSecurityException thrown if an Security error ocurred. 
   */ 
  public static X509Certificate generateCertificate(String dn, KeyPair pair,
                                                    int days, String algorithm)
    throws GeneralSecurityException, IOException {
    PrivateKey privkey = pair.getPrivate();
    X509CertInfo info = new X509CertInfo();
    Date from = new Date();
    Date to = new Date(from.getTime() + days * 86400000l);
    CertificateValidity interval = new CertificateValidity(from, to);
    BigInteger sn = new BigInteger(64, new SecureRandom());
    X500Name owner = new X500Name(dn);
 
    info.set(X509CertInfo.VALIDITY, interval);
    info.set(X509CertInfo.SERIAL_NUMBER, new CertificateSerialNumber(sn));
    info.set(X509CertInfo.SUBJECT, new CertificateSubjectName(owner));
    info.set(X509CertInfo.ISSUER, new CertificateIssuerName(owner));
    info.set(X509CertInfo.KEY, new CertificateX509Key(pair.getPublic()));
    info
      .set(X509CertInfo.VERSION, new CertificateVersion(CertificateVersion.V3));

7
CodeRank
This code example shows how to use the following methods:getPrivate, getPublic
		String baseString = "thisismysignaturebasestringthatshouldbemuchlongerthanthisbutitdoesnthavetobeandherearesomestrangecharacters!@#$%^&*)(*";
 
		byte[] signatureBytes;
		{ 
			Signature signer = Signature.getInstance("SHA1withRSA");
			signer.initSign(keyPair.getPrivate());
			signer.update(baseString.getBytes("UTF-8"));
			signatureBytes = signer.sign();
		} 
 
		{ 
			Signature signer = Signature.getInstance("SHA1withRSA");
			signer.initVerify(keyPair.getPublic());
			signer.update(baseString.getBytes("UTF-8"));
			assertTrue(signer.verify(signatureBytes));
		} 
 
		RSA_SHA1SignatureMethod signatureMethod = new RSA_SHA1SignatureMethod(keyPair.getPrivate(), keyPair.getPublic());
		String signature = signatureMethod.sign(baseString);
		signatureMethod.verify(baseString, signature);

7
CodeRank

Related Java 8 KeyPair Questions & Answers:

See Code Examples for Java 8 KeyPair Methods: