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)));
}
}
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);
}
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;
}
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;
}
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;
}
Aggregations