Key getFormat - top ranked examples from Open Source projects

These code examples were ranked by Codota’s semantic indexing as the best open source examples for Key getFormat method.

This code example shows how to use the following methods:getClass, getAlgorithm, getEncoded, getFormat
 
        if (keySpec == null) {
            throw new InvalidKeySpecException("keySpec == null");
        } 
 
        if (!"EC".equals(key.getAlgorithm())) {
            throw new InvalidKeySpecException("Key must be an EC key");
        } 
 
        if (key instanceof ECPublicKey && ECPublicKeySpec.class.isAssignableFrom(keySpec)) {
            ECPublicKey ecKey = (ECPublicKey) key;
            return (T) new ECPublicKeySpec(ecKey.getW(), ecKey.getParams());
        } else if (key instanceof PublicKey && ECPublicKeySpec.class.isAssignableFrom(keySpec)) {
            final byte[] encoded = key.getEncoded();
            if (!"X.509".equals(key.getFormat()) || encoded == null) {
                throw new InvalidKeySpecException("Not a valid X.509 encoding");
            } 
            ECPublicKey ecKey = (ECPublicKey) engineGeneratePublic(new X509EncodedKeySpec(encoded));
            return (T) new ECPublicKeySpec(ecKey.getW(), ecKey.getParams());
        } else if (key instanceof ECPrivateKey

7
CodeRank
This code example shows how to use the following methods:getAlgorithm, getEncoded, getFormat
    private void encodeKey( 
        Key                 key,
        DataOutputStream    dOut)
        throws IOException 
    { 
        byte[]      enc = key.getEncoded();
 
        if (key instanceof PrivateKey)
        { 
            dOut.write(KEY_PRIVATE);
        } 
        else if (key instanceof PublicKey)
        { 
            dOut.write(KEY_PUBLIC);
        } 
        else 
        { 
            dOut.write(KEY_SECRET);
        } 
     

7
CodeRank
This code example shows how to use the following methods:getClass, getAlgorithm, getEncoded, getFormat
 
        if (keySpec == null) {
            throw new InvalidKeySpecException("keySpec == null");
        } 
 
        if (!"RSA".equals(key.getAlgorithm())) {
            throw new InvalidKeySpecException("Key must be a RSA key");
        } 
 
        if (key instanceof RSAPublicKey && RSAPublicKeySpec.class.isAssignableFrom(keySpec)) {
            RSAPublicKey rsaKey = (RSAPublicKey) key;
            return (T) new RSAPublicKeySpec(rsaKey.getModulus(), rsaKey.getPublicExponent());
        } else if (key instanceof PublicKey && RSAPublicKeySpec.class.isAssignableFrom(keySpec)) {
            final byte[] encoded = key.getEncoded();
            if (!"X.509".equals(key.getFormat()) || encoded == null) {
                throw new InvalidKeySpecException("Not a valid X.509 encoding");
            } 
            RSAPublicKey rsaKey =
                    (RSAPublicKey) engineGeneratePublic(new X509EncodedKeySpec(encoded));
            return (T) new RSAPublicKeySpec(rsaKey.getModulus(), rsaKey.getPublicExponent());

7
CodeRank
This code example shows how to use the following methods:toString, getAlgorithm, getEncoded, getFormat
This code example shows how to use the following methods:getAlgorithm, getFormat
        KeyFactory keyFactory
        = KeyFactory.getInstance("RSA");
        PrivateKey privateKey
        = keyFactory.generatePrivate(privateKeySpec);
         
        System.out.println("Loaded " + privateKey.getAlgorithm()
                   + " " + privateKey.getFormat()
                   + " private key."); 
        return privateKey;
    } finally { 
        // Dynamically deinstall the RSA provider  
        Security.removeProvider(provider.getName());
    } 
    } 
     
    /* ------------------------------------------------------------ */ 
    /** 
     * Show a usage message. 
     */ 
    static private void usage() 
6
CodeRank
This code example shows how to use the following methods:getAlgorithm, getEncoded, getFormat
    public void testLoadGATKDistributedPublicKey() { 
        PublicKey gatkDistributedPublicKey = CryptUtils.loadGATKDistributedPublicKey();
    } 
 
    private void assertKeysAreEqual( Key originalKey, Key keyFromDisk ) {
        Assert.assertTrue(Arrays.equals(originalKey.getEncoded(), keyFromDisk.getEncoded()));
        Assert.assertEquals(originalKey.getAlgorithm(), keyFromDisk.getAlgorithm());
        Assert.assertEquals(originalKey.getFormat(), keyFromDisk.getFormat());
    } 
 
    private boolean gatkPrivateKeyExistsButReadPermissionDenied() { 
        File gatkPrivateKey = new File(CryptUtils.GATK_MASTER_PRIVATE_KEY_FILE);
        return gatkPrivateKey.exists() && ! gatkPrivateKey.canRead();
    } 
} 
6
CodeRank
This code example shows how to use the following methods:getAlgorithm, getEncoded, getFormat
    private void encodeKey( 
        Key                 key,
        DataOutputStream    dOut)
        throws IOException 
    { 
        byte[]      enc = key.getEncoded();
 
        if (key instanceof PrivateKey)
        { 
            dOut.write(KEY_PRIVATE);
        } 
        else if (key instanceof PublicKey)
        { 
            dOut.write(KEY_PUBLIC);
        } 
        else 
        { 
            dOut.write(KEY_SECRET);
        } 
     

6
CodeRank
This code example shows how to use the following methods:getEncoded, getFormat
    protected KeySpec engineGetKeySpec(
        Key key,
        Class spec)
        throws InvalidKeySpecException 
    { 
        if (spec.isAssignableFrom(PKCS8EncodedKeySpec.class) && key.getFormat().equals("PKCS#8"))
        { 
            return new PKCS8EncodedKeySpec(key.getEncoded());
        } 
        else if (spec.isAssignableFrom(X509EncodedKeySpec.class) && key.getFormat().equals("X.509"))
        { 
            return new X509EncodedKeySpec(key.getEncoded());
        } 
 
        throw new InvalidKeySpecException("not implemented yet " + key + " " + spec);
    } 
} 
6
CodeRank
This code example shows how to use the following methods:getAlgorithm, getEncoded, getFormat
  } 
 
  protected SecretKey engineTranslateKey(SecretKey key)
      throws InvalidKeyException 
  { 
    if (! "RAW".equals(key.getFormat()))
      throw new InvalidKeyException("only raw keys are supported");
    // SecretKeySpec is good enough for our purposes. 
    return new SecretKeySpec(key.getEncoded(), key.getAlgorithm());
  } 
} 
6
CodeRank
This code example shows how to use the following methods:getClass, getAlgorithm, getEncoded, getFormat
 
        if (keySpec == null) {
            throw new InvalidKeySpecException("keySpec == null");
        } 
 
        if (!"EC".equals(key.getAlgorithm())) {
            throw new InvalidKeySpecException("Key must be an EC key");
        } 
 
        if (key instanceof ECPublicKey && ECPublicKeySpec.class.isAssignableFrom(keySpec)) {
            ECPublicKey ecKey = (ECPublicKey) key;
            return (T) new ECPublicKeySpec(ecKey.getW(), ecKey.getParams());
        } else if (key instanceof PublicKey && ECPublicKeySpec.class.isAssignableFrom(keySpec)) {
            final byte[] encoded = key.getEncoded();
            if (!"X.509".equals(key.getFormat()) || encoded == null) {
                throw new InvalidKeySpecException("Not a valid X.509 encoding");
            } 
            ECPublicKey ecKey = (ECPublicKey) engineGeneratePublic(new X509EncodedKeySpec(encoded));
            return (T) new ECPublicKeySpec(ecKey.getW(), ecKey.getParams());
        } else if (key instanceof ECPrivateKey

7
CodeRank
getFormat Method Overview
Returns the name of the primary encoding format of this key, or null if this key does not support encoding.
See Code Examples for other Java 8 Key Methods: