Search in sources :

Example 46 with Base64.encodeBase64String

use of org.apache.commons.codec.binary.Base64.encodeBase64String in project ovirt-engine by oVirt.

the class EngineEncryptionUtils method encrypt.

/**
 * Encrypt string.
 * @param source string to encrypt.
 * @return encrypted string.
 * @throws GeneralSecurityException
 * Please note that empty strings are not encrypted and are returned as-is.
 */
public static String encrypt(String source) throws GeneralSecurityException {
    if (source == null || source.length() == 0) {
        return source;
    } else {
        Cipher rsa = Cipher.getInstance("RSA");
        rsa.init(Cipher.ENCRYPT_MODE, getCertificate().getPublicKey());
        return new Base64(0).encodeToString(rsa.doFinal(source.getBytes(StandardCharsets.UTF_8)));
    }
}
Also used : Base64(org.apache.commons.codec.binary.Base64) Cipher(javax.crypto.Cipher)

Example 47 with Base64.encodeBase64String

use of org.apache.commons.codec.binary.Base64.encodeBase64String in project ovirt-engine by oVirt.

the class Ticketing method generateOTP.

public static String generateOTP() {
    SecureRandom secr = new SecureRandom();
    byte[] arrRandom = new byte[9];
    secr.nextBytes(arrRandom);
    // encode password into Base64 text:
    return new Base64(0).encodeToString(arrRandom);
}
Also used : Base64(org.apache.commons.codec.binary.Base64) SecureRandom(java.security.SecureRandom)

Example 48 with Base64.encodeBase64String

use of org.apache.commons.codec.binary.Base64.encodeBase64String in project ovirt-engine by oVirt.

the class LoginOnBehalfCommand method createSession.

private String createSession(DbUser mappedUser, String authzName, ExtMap principalRecord) {
    directoryUtils.flatGroups(principalRecord);
    DbUser dbUser = directoryUtils.mapPrincipalRecordToDbUser(authzName, principalRecord);
    dbUser.setId(mappedUser.getId());
    String engineSessionId;
    byte[] s = new byte[64];
    new SecureRandom().nextBytes(s);
    engineSessionId = new Base64(0).encodeToString(s);
    sessionDataContainer.setUser(engineSessionId, dbUser);
    sessionDataContainer.refresh(engineSessionId);
    return engineSessionId;
}
Also used : Base64(org.apache.commons.codec.binary.Base64) SecureRandom(java.security.SecureRandom) DbUser(org.ovirt.engine.core.common.businessentities.aaa.DbUser)

Example 49 with Base64.encodeBase64String

use of org.apache.commons.codec.binary.Base64.encodeBase64String in project ovirt-engine by oVirt.

the class SessionDataContainer method generateEngineSessionId.

public String generateEngineSessionId() {
    String engineSessionId;
    byte[] s = new byte[64];
    new SecureRandom().nextBytes(s);
    engineSessionId = new Base64(0).encodeToString(s);
    return engineSessionId;
}
Also used : Base64(org.apache.commons.codec.binary.Base64) SecureRandom(java.security.SecureRandom)

Example 50 with Base64.encodeBase64String

use of org.apache.commons.codec.binary.Base64.encodeBase64String in project ovirt-engine by oVirt.

the class OpenSSHUtils method getKeyString.

/**
 * Convert a public key to the SSH format used in the <code>authorized_keys</code> files.
 *
 * Note that only RSA keys are supported at the moment.
 *
 * @param key
 *            the public key to convert
 * @param alias
 *            the alias to be appended at the end of the line, if it is <code>null</code> nothing will be appended
 * @return an string that can be directly written to the <code>authorized_keys</code> file or <code>null</code> if
 *         the conversion can't be performed for whatever the reason
 */
public static String getKeyString(final PublicKey key, String alias) {
    // Get the serialized version of the key:
    final byte[] keyBytes = getKeyBytes(key);
    if (keyBytes == null) {
        log.error("Can't get key bytes, will return null.");
        return null;
    }
    // Encode it using BASE64:
    final Base64 encoder = new Base64(0);
    final String encoding = encoder.encodeToString(keyBytes);
    if (log.isDebugEnabled()) {
        log.debug("Key encoding is '{}'.", encoding);
    }
    // Return the generated SSH public key:
    final StringBuilder buffer = new StringBuilder(SSH_RSA.length() + 1 + encoding.length() + (alias != null ? 1 + alias.length() : 0) + 1);
    buffer.append(SSH_RSA);
    buffer.append(" ");
    buffer.append(encoding);
    if (alias != null) {
        buffer.append(" ");
        buffer.append(alias);
    }
    buffer.append('\n');
    final String keyString = buffer.toString();
    if (log.isDebugEnabled()) {
        log.debug("Key string is '{}'.", keyString);
    }
    return keyString;
}
Also used : Base64(org.apache.commons.codec.binary.Base64)

Aggregations

Base64 (org.apache.commons.codec.binary.Base64)135 IOException (java.io.IOException)30 Test (org.junit.Test)29 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)12 InputStream (java.io.InputStream)11 HttpServletRequest (javax.servlet.http.HttpServletRequest)11 HttpServletResponse (javax.servlet.http.HttpServletResponse)11 Base64.encodeBase64String (org.apache.commons.codec.binary.Base64.encodeBase64String)11 ByteArrayInputStream (java.io.ByteArrayInputStream)10 HashMap (java.util.HashMap)10 SecretKeySpec (javax.crypto.spec.SecretKeySpec)9 MessageDigest (java.security.MessageDigest)8 File (java.io.File)7 UnsupportedEncodingException (java.io.UnsupportedEncodingException)7 URL (java.net.URL)7 Mac (javax.crypto.Mac)7 ServletException (javax.servlet.ServletException)7 X509Certificate (java.security.cert.X509Certificate)6 FileNotFoundException (java.io.FileNotFoundException)5 Signature (java.security.Signature)5