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

This code example shows how to use the following methods:getEncoded
  public MasterKeyData(int serialNo, SecretKey secretKey) {
    this.masterKeyRecord = Records.newRecord(MasterKey.class);
    this.masterKeyRecord.setKeyId(serialNo);
    this.generatedSecretKey = secretKey;
    this.masterKeyRecord.setBytes(ByteBuffer.wrap(generatedSecretKey
      .getEncoded()));
  } 
 
  public MasterKeyData(MasterKey masterKeyRecord, SecretKey secretKey) {
    this.masterKeyRecord = masterKeyRecord;
    this.generatedSecretKey = secretKey;
 
  } 
 
  public MasterKey getMasterKey() { 
    return this.masterKeyRecord;
  } 
 
  public SecretKey getSecretKey() {
    return this.generatedSecretKey;
7
CodeRank
Experience pair programming with AI  Get Codota for Java
This code example shows how to use the following methods:getEncoded
KeyGenerator kgen = KeyGenerator.getInstance("AES");  
	    SecureRandom sr = SecureRandom.getInstance("SHA1PRNG");  
	    kgen.init(m_nKeyLenBit, sr);  
	    SecretKey skey = kgen.generateKey();  
	    m_dbKeyData = skey.getEncoded();
	    Logger.I(TAG, "New key is generated");
	    if(DEBUG)
	        Logger.I(TAG, "Key: " + dumpKey(m_dbKeyData));
	} 
 
    private void writeKeyToStorage() 
    { 
        SharedPreferences settings =  ContextFactory.getContext().getSharedPreferences(m_strPrefName, Context.MODE_PRIVATE);		
        SharedPreferences.Editor editor = settings.edit();
        String strKey = Base64.encodeToString(m_dbKeyData, false);//, Base64.DEFAULT);
         
        editor.putString(m_strDBPartition, strKey );
        if (editor.commit()) {
            Logger.I(TAG, "Key is successfully saved to Shared Preferences");
7
CodeRank
Experience pair programming with AI  Get Codota for Java
This code example shows how to use the following methods:getAlgorithm
    return skf.generateSecret(keyspec);
  } 
 
  private static Cipher getCipherFromPassphrase(String passphrase, byte[] salt, int opMode) throws GeneralSecurityException {
    SecretKey key              = getKeyFromPassphrase(passphrase, salt);
    Cipher cipher              = Cipher.getInstance(key.getAlgorithm());
    cipher.init(opMode, key, new PBEParameterSpec(salt, 100));
 
    return cipher;
  } 
 
  private static byte[] encryptWithPassphrase(Context context, byte[] data, String passphrase) throws NoSuchAlgorithmException, GeneralSecurityException {
    byte[] encryptionSalt = generateSalt();
    Cipher cipher         = getCipherFromPassphrase(passphrase, encryptionSalt, Cipher.ENCRYPT_MODE);
    byte[] cipherText     = cipher.doFinal(data);
 
    save(context, "encryption_salt", encryptionSalt);
    return cipherText;
  } 
 
7
CodeRank
Experience pair programming with AI  Get Codota for Java
This code example shows how to use the following methods:getEncoded
         
            final KeySpec keySpec = new PBEKeySpec(input);
            final SecretKeyFactory keyFactory = 
                SecretKeyFactory.getInstance(getAlgorithm());
            final SecretKey key = keyFactory.generateSecret(keySpec);
            rawKey = key.getEncoded();
 
            // if initializing, just set it 
            if (init) {
                validator = createValidator(rawKey);
                storeValidator(validator);
                feedLocalKey(key);
            } 
            // if not initializing, check validity before setting 
            else { 
                if (validator == null) {
                    validator = loadValidator();
                } 
                if (checkKeyValid(rawKey, validator)) {
                    feedLocalKey(key);

6
CodeRank
Experience pair programming with AI  Get Codota for Java
This code example shows how to use the following methods:getEncoded
  public DelegationKey() { 
    this(0, 0L, (SecretKey)null);
  } 
 
  public DelegationKey(int keyId, long expiryDate, SecretKey key) {
    this(keyId, expiryDate, key != null ? key.getEncoded() : null);
  } 
   
  public DelegationKey(int keyId, long expiryDate, byte[] encodedKey) {
    this.keyId = keyId;
    this.expiryDate = expiryDate;
    if (encodedKey != null) {
      if (encodedKey.length > MAX_KEY_LEN) {
        throw new RuntimeException("can't create " + encodedKey.length +
            " byte long DelegationKey."); 
      } 
      this.keyBytes = encodedKey;
    } 
  } 
 
7
CodeRank
Experience pair programming with AI  Get Codota for Java
This code example shows how to use the following methods:getAlgorithm, getEncoded
        if (key == null)
        { 
            throw new InvalidKeyException("key parameter is null");
        } 
         
        if (!key.getAlgorithm().equalsIgnoreCase(algName))
        { 
            throw new InvalidKeyException("Key not of type " + algName + ".");
        } 
 
        return new SecretKeySpec(key.getEncoded(), algName);
    } 
} 
6
CodeRank
Experience pair programming with AI  Get Codota for Java
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
Experience pair programming with AI  Get Codota for Java
This code example shows how to use the following methods:getEncoded
    } 
 
    protected String encryptMessage(String secureRequest, SecretKey sessionKey)
        throws EncryptionException, InterruptedException, IOException { 
        PublicKey publicKey = getPublicKeyFromConfigFile();
        String encodedMessage = Base64.encodeBase64String(CipherUtils.encrypt(secureRequest.getBytes(), sessionKey));
        String encodedKey = Base64.encodeBase64String(CipherUtils.encrypt(sessionKey.getEncoded(), publicKey));
 
        String encryptedMessage = ""
                + "{" 
                + "  \"encryptedContent\":\"" + encodedMessage + "\","
                + "  \"encryptedKey\":\"" + encodedKey + "\""
                + "}"; 
        return encryptedMessage;
    } 
 
    protected SecretKey generateSessionKey() {
        SecretKey sessionKey =
            CipherUtils.generateKey(CipherUtils.DEFAULT_SYMMETRIC_ALGORITHM, CipherUtils.DEFAULT_SYMMETRIC_KEYSIZE); 
        return sessionKey;
6
CodeRank
Experience pair programming with AI  Get Codota for Java
This code example shows how to use the following methods:getEncoded
            if (init) {
                // generate a new key 
                final KeyGenerator kgen = KeyGenerator.getInstance(getAlgorithm());
                kgen.init(getKeyLength());
                final SecretKey key = kgen.generateKey();
                rawKey = key.getEncoded();
                storeKeyData(rawKey);
                byte [] validator = createValidator(rawKey);
                storeValidator(validator);
            } 
            else { 
                rawKey = loadKeyData();
                if (!checkKeyValid(rawKey, loadValidator())) {
                    throw new GeneralSecurityException("Stored key is incorrect or corrupt.");
                } 
            } 
            return new SecretKeySpec(rawKey, getAlgorithm());
        } finally { 
            zeroFill(rawKey);
        } 
6
CodeRank
Experience pair programming with AI  Get Codota for Java
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
Experience pair programming with AI  Get Codota for Java

Related Java 8 SecretKey Questions & Answers:

See Code Examples for Java 8 SecretKey Methods: