Java Code Examples for org.apache.axiom.om.util.Base64#encode()

The following examples show how to use org.apache.axiom.om.util.Base64#encode() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: KeyStoreAdminClient.java    From carbon-identity-framework with Apache License 2.0 6 votes vote down vote up
public void addTrustStore(byte[] content, String filename, String password, String provider,
                          String type) {

    try {
        String data = Base64.encode(content);
        AddTrustStore request = new AddTrustStore();

        request.setFileData(data);
        request.setFilename(filename);
        request.setPassword(password);
        request.setProvider(provider);
        request.setType(type);
        stub.addTrustStore(request);
    } catch (java.lang.Exception e) {
        log.error("Error in adding truststore", e);
    }
}
 
Example 2
Source File: LDAPServerStoreManager.java    From carbon-identity with Apache License 2.0 6 votes vote down vote up
private String getPasswordToStore(String password, String passwordHashMethod)
        throws DirectoryServerManagerException {

    String passwordToStore = password;

    if (passwordHashMethod != null) {
        try {

            if (passwordHashMethod.equals(LDAPServerManagerConstants.PASSWORD_HASH_METHOD_PLAIN_TEXT)) {
                return passwordToStore;
            }

            MessageDigest messageDigest = MessageDigest.getInstance(passwordHashMethod);
            byte[] digestValue = messageDigest.digest(password.getBytes(StandardCharsets.UTF_8));
            passwordToStore = "{" + passwordHashMethod + "}" + Base64.encode(digestValue);

        } catch (NoSuchAlgorithmException e) {
            throw new DirectoryServerManagerException("Invalid hashMethod", e);
        }
    }

    return passwordToStore;
}
 
Example 3
Source File: Util.java    From carbon-identity-framework with Apache License 2.0 6 votes vote down vote up
public static String preparePassword(String password, String saltValue) throws UserStoreException {
    try {
        String digestInput = password;
        if (saltValue != null) {
            digestInput = password + saltValue;
        }
        String digsestFunction = Util.getRealmConfig().getUserStoreProperties()
                .get(JDBCRealmConstants.DIGEST_FUNCTION);
        if (digsestFunction != null) {

            if (digsestFunction.equals(UserCoreConstants.RealmConfig.PASSWORD_HASH_METHOD_PLAIN_TEXT)) {
                return password;
            }

            MessageDigest dgst = MessageDigest.getInstance(digsestFunction);
            byte[] byteValue = dgst.digest(digestInput.getBytes(Charset.forName("UTF-8")));
            password = Base64.encode(byteValue);
        }
        return password;
    } catch (NoSuchAlgorithmException e) {
        log.error(e.getMessage(), e);
        throw new UserStoreException(e.getMessage(), e);
    }
}
 
Example 4
Source File: KeyStoreAdminClient.java    From carbon-identity-framework with Apache License 2.0 6 votes vote down vote up
public void addKeyStore(byte[] content, String filename, String password, String provider,
                        String type, String pvtkspass) throws java.lang.Exception {

    try {
        String data = Base64.encode(content);
        AddKeyStore request = new AddKeyStore();
        request.setFileData(data);
        request.setFilename(filename);
        request.setPassword(password);
        request.setProvider(provider);
        request.setType(type);
        request.setPvtkeyPass(pvtkspass);
        stub.addKeyStore(request);
    } catch (java.lang.Exception e) {
        log.error("Error in adding keystore", e);
        throw e;
    }
}
 
Example 5
Source File: Util.java    From carbon-identity with Apache License 2.0 6 votes vote down vote up
public static String preparePassword(String password, String saltValue) throws UserStoreException {
    try {
        String digestInput = password;
        if (saltValue != null) {
            digestInput = password + saltValue;
        }
        String digsestFunction = Util.getRealmConfig().getUserStoreProperties()
                .get(JDBCRealmConstants.DIGEST_FUNCTION);
        if (digsestFunction != null) {

            if (digsestFunction.equals(UserCoreConstants.RealmConfig.PASSWORD_HASH_METHOD_PLAIN_TEXT)) {
                return password;
            }

            MessageDigest dgst = MessageDigest.getInstance(digsestFunction);
            byte[] byteValue = dgst.digest(digestInput.getBytes(Charset.forName("UTF-8")));
            password = Base64.encode(byteValue);
        }
        return password;
    } catch (NoSuchAlgorithmException e) {
        log.error(e.getMessage(), e);
        throw new UserStoreException(e.getMessage(), e);
    }
}
 
Example 6
Source File: LDAPServerStoreManager.java    From carbon-identity-framework with Apache License 2.0 6 votes vote down vote up
private String getPasswordToStore(String password, String passwordHashMethod)
        throws DirectoryServerManagerException {

    String passwordToStore = password;

    if (passwordHashMethod != null) {
        try {

            if (passwordHashMethod.equals(LDAPServerManagerConstants.PASSWORD_HASH_METHOD_PLAIN_TEXT)) {
                return passwordToStore;
            }

            MessageDigest messageDigest = MessageDigest.getInstance(passwordHashMethod);
            byte[] digestValue = messageDigest.digest(password.getBytes(StandardCharsets.UTF_8));
            passwordToStore = "{" + passwordHashMethod + "}" + Base64.encode(digestValue);

        } catch (NoSuchAlgorithmException e) {
            throw new DirectoryServerManagerException("Invalid hashMethod", e);
        }
    }

    return passwordToStore;
}
 
Example 7
Source File: Util.java    From carbon-identity with Apache License 2.0 6 votes vote down vote up
public static String getSaltValue() {
    String saltValue = null;
    if ("true".equals(realmConfig.getUserStoreProperties().get(JDBCRealmConstants.STORE_SALTED_PASSWORDS))) {
        try {
            SecureRandom secureRandom = SecureRandom.getInstance(SHA_1_PRNG);
            byte[] bytes = new byte[16];
            //secureRandom is automatically seeded by calling nextBytes
            secureRandom.nextBytes(bytes);
            saltValue = Base64.encode(bytes);
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException("SHA1PRNG algorithm could not be found.", e);
        }

    }
    return saltValue;
}
 
Example 8
Source File: KeyStoreAdminClient.java    From carbon-identity with Apache License 2.0 6 votes vote down vote up
public void addKeyStore(byte[] content, String filename, String password, String provider,
                        String type, String pvtkspass) throws java.lang.Exception {
    try {
        String data = Base64.encode(content);
        AddKeyStore request = new AddKeyStore();
        request.setFileData(data);
        request.setFilename(filename);
        request.setPassword(password);
        request.setProvider(provider);
        request.setType(type);
        request.setPvtkeyPass(pvtkspass);
        stub.addKeyStore(request);
    } catch (java.lang.Exception e) {
        log.error("Error in adding keystore", e);
        throw e;
    }
}
 
Example 9
Source File: SecondaryUserStoreConfigurationUtil.java    From carbon-identity with Apache License 2.0 5 votes vote down vote up
/**
 * @param plainText Cipher text to be encrypted
 * @return Returns the encrypted text
 * @throws IdentityUserStoreMgtException Encryption failed
 */
public static String encryptPlainText(String plainText) throws IdentityUserStoreMgtException {

    if (cipher == null) {
        initializeKeyStore();
    }

    try {
        return Base64.encode(cipher.doFinal((plainText.getBytes())));
    } catch (GeneralSecurityException e) {
        String errMsg = "Failed to generate the cipher text";
        throw new IdentityUserStoreMgtException(errMsg, e);
    }
}
 
Example 10
Source File: JDBCUserStoreManager.java    From micro-integrator with Apache License 2.0 5 votes vote down vote up
/**
 * This private method returns a saltValue using SecureRandom.
 *
 * @return saltValue
 */
private String generateSaltValue() {
    String saltValue = null;
    try {
        SecureRandom secureRandom = SecureRandom.getInstance(SHA_1_PRNG);
        byte[] bytes = new byte[16];
        //secureRandom is automatically seeded by calling nextBytes
        secureRandom.nextBytes(bytes);
        saltValue = Base64.encode(bytes);
    } catch (NoSuchAlgorithmException e) {
        throw new RuntimeException("SHA1PRNG algorithm could not be found.");
    }
    return saltValue;
}
 
Example 11
Source File: Utils.java    From carbon-identity with Apache License 2.0 5 votes vote down vote up
/**
 * @param value
 * @return
 * @throws UserStoreException
 */
public static String doHash(String value) throws UserStoreException {
    try {
        String digsestFunction = "SHA-256";
        MessageDigest dgst = MessageDigest.getInstance(digsestFunction);
        byte[] byteValue = dgst.digest(value.getBytes());
        return Base64.encode(byteValue);
    } catch (NoSuchAlgorithmException e) {
        log.error(e.getMessage(), e);
        throw new UserStoreException(e.getMessage(), e);
    }
}
 
Example 12
Source File: Utils.java    From carbon-identity-framework with Apache License 2.0 5 votes vote down vote up
/**
 * @param value
 * @return
 * @throws UserStoreException
 */
public static String doHash(String value) throws UserStoreException {
    try {
        String digsestFunction = "SHA-256";
        MessageDigest dgst = MessageDigest.getInstance(digsestFunction);
        byte[] byteValue = dgst.digest(value.getBytes());
        return Base64.encode(byteValue);
    } catch (NoSuchAlgorithmException e) {
        log.error(e.getMessage(), e);
        throw new UserStoreException(e.getMessage(), e);
    }
}
 
Example 13
Source File: UserStoreConfigurationDeployer.java    From carbon-identity with Apache License 2.0 5 votes vote down vote up
/**
 * Encrypts the secondary user store configuration
 *
 * @param secondaryStoreDocument OMElement of respective file path
 * @param cipher                 Cipher object read for super-tenant's key store
 * @throws UserStoreConfigurationDeployerException If update operation failed
 */
private void updateSecondaryUserStore(OMElement secondaryStoreDocument, Cipher cipher) throws
        UserStoreConfigurationDeployerException {
    String className = secondaryStoreDocument.getAttributeValue(new QName(UserStoreConfigurationConstants.PROPERTY_CLASS));
    ArrayList<String> encryptList = getEncryptPropertyList(className);
    Iterator<?> ite = secondaryStoreDocument.getChildrenWithName(new QName(UserStoreConfigurationConstants.PROPERTY));
    while (ite.hasNext()) {
        OMElement propElem = (OMElement) ite.next();

        if (propElem != null && (propElem.getText() != null)) {
            String propertyName = propElem.getAttributeValue(new QName(UserStoreConfigurationConstants.PROPERTY_NAME));

            OMAttribute encryptedAttr = propElem.getAttribute(new QName(UserStoreConfigurationConstants
                    .PROPERTY_ENCRYPTED));
            if (encryptedAttr == null) {
                boolean encrypt = encryptList.contains(propertyName) || isEligibleTobeEncrypted(propElem);
                if (encrypt) {
                    OMAttribute encryptAttr = propElem.getAttribute(new QName(UserStoreConfigurationConstants.PROPERTY_ENCRYPT));
                    if (encryptAttr != null) {
                        propElem.removeAttribute(encryptAttr);
                    }

                    try {
                        String cipherText = Base64.encode(cipher.doFinal((propElem.getText().getBytes())));
                        propElem.setText(cipherText);
                        propElem.addAttribute(UserStoreConfigurationConstants.PROPERTY_ENCRYPTED, "true", null);
                    } catch (GeneralSecurityException e) {
                        String errMsg = "Encryption in secondary user store failed";
                        throw new UserStoreConfigurationDeployerException(errMsg, e);
                    }
                }
            }
        }
    }
}
 
Example 14
Source File: UserStoreConfigurationDeployer.java    From carbon-identity-framework with Apache License 2.0 5 votes vote down vote up
/**
 * Encrypts the secondary user store configuration
 *
 * @param secondaryStoreDocument OMElement of respective file path
 * @throws UserStoreConfigurationDeployerException If update operation failed
 */
private void updateSecondaryUserStore(OMElement secondaryStoreDocument) throws
        UserStoreConfigurationDeployerException {
    String className = secondaryStoreDocument.getAttributeValue(new QName(UserStoreConfigurationConstants.PROPERTY_CLASS));
    ArrayList<String> encryptList = getEncryptPropertyList(className);
    Iterator<?> ite = secondaryStoreDocument.getChildrenWithName(new QName(UserStoreConfigurationConstants.PROPERTY));
    while (ite.hasNext()) {
        OMElement propElem = (OMElement) ite.next();

        if (propElem != null && (propElem.getText() != null)) {
            String propertyName = propElem.getAttributeValue(new QName(UserStoreConfigurationConstants.PROPERTY_NAME));

            OMAttribute encryptedAttr = propElem.getAttribute(new QName(UserStoreConfigurationConstants
                    .PROPERTY_ENCRYPTED));
            if (encryptedAttr == null) {
                boolean encrypt = encryptList.contains(propertyName) || isEligibleTobeEncrypted(propElem);
                if (encrypt) {
                    OMAttribute encryptAttr = propElem.getAttribute(new QName(UserStoreConfigurationConstants.PROPERTY_ENCRYPT));
                    if (encryptAttr != null) {
                        propElem.removeAttribute(encryptAttr);
                    }

                    try {
                        String cipherText = Base64.encode(UserStoreUtil.encrypt((propElem.getText().getBytes())));
                        propElem.setText(cipherText);
                        propElem.addAttribute(UserStoreConfigurationConstants.PROPERTY_ENCRYPTED, "true", null);
                    } catch (CryptoException e) {
                        String errMsg = "Encryption in secondary user store failed";
                        throw new UserStoreConfigurationDeployerException(errMsg, e);
                    }
                }
            }
        }
    }
}
 
Example 15
Source File: IdPManagementUIUtil.java    From carbon-identity-framework with Apache License 2.0 5 votes vote down vote up
private static String handleCertificateDeletion(String oldCertificateValues, String deletedCertificateValues) {

        String decodedOldCertificate = new String(Base64.decode(oldCertificateValues), StandardCharsets.UTF_8);
        String decodedDeletedCertificate = new String(Base64.decode(deletedCertificateValues), StandardCharsets.UTF_8);

        Set<String> updatedCertificateSet = new LinkedHashSet<>(getExtractedCertificateValues(decodedOldCertificate));
        updatedCertificateSet.removeAll(getExtractedCertificateValues(decodedDeletedCertificate));
        return Base64.encode(String.join("", updatedCertificateSet).getBytes(StandardCharsets.UTF_8));
    }
 
Example 16
Source File: KeyStoreAdminClient.java    From carbon-identity with Apache License 2.0 5 votes vote down vote up
public void importCertToStore(String filename, byte[] content, String keyStoreName)
        throws java.lang.Exception {
    try {
        String data = Base64.encode(content);
        ImportCertToStore request = new ImportCertToStore();
        request.setFileName(filename);
        request.setFileData(data);
        request.setKeyStoreName(keyStoreName);
        stub.importCertToStore(request);
    } catch (java.lang.Exception e) {
        log.error("Error in importing cert to store.", e);
        throw e;
    }
}
 
Example 17
Source File: JDBCUserStoreManager.java    From micro-integrator with Apache License 2.0 5 votes vote down vote up
/**
 * @param password
 * @param saltValue
 * @return
 * @throws UserStoreException
 */
@Deprecated
protected String preparePassword(String password, String saltValue) throws UserStoreException {
    try {
        String digestInput = password;
        if (saltValue != null) {
            digestInput = password + saltValue;
        }
        String digsestFunction = realmConfig.getUserStoreProperties().get(
                JDBCRealmConstants.DIGEST_FUNCTION);
        if (digsestFunction != null) {

            if (digsestFunction
                    .equals(UserCoreConstants.RealmConfig.PASSWORD_HASH_METHOD_PLAIN_TEXT)) {
                return password;
            }

            MessageDigest dgst = MessageDigest.getInstance(digsestFunction);
            byte[] byteValue = dgst.digest(digestInput.getBytes());
            password = Base64.encode(byteValue);
        }
        return password;
    } catch (NoSuchAlgorithmException e) {
        String msg = "Error occurred while preparing password.";
        if (log.isDebugEnabled()) {
            log.debug(msg, e);
        }
        throw new UserStoreException(msg, e);
    }
}
 
Example 18
Source File: SSOAgentSampleFilter.java    From msf4j with Apache License 2.0 4 votes vote down vote up
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse,
                     FilterChain filterChain) throws IOException, ServletException {

    String httpBinding = servletRequest.getParameter(
            SSOAgentConstants.SSOAgentConfig.SAML2.HTTP_BINDING);
    if (httpBinding != null && !httpBinding.isEmpty()) {
        if ("HTTP-POST".equals(httpBinding)) {
            httpBinding = "urn:oasis:names:tc:SAML:2.0:bindings:HTTP-POST";
        } else if ("HTTP-Redirect".equals(httpBinding)) {
            httpBinding = "urn:oasis:names:tc:SAML:2.0:bindings:HTTP-Redirect";
        } else {
            LOGGER.log(Level.INFO, "Unknown SAML2 HTTP Binding. Defaulting to HTTP-POST");
            httpBinding = "urn:oasis:names:tc:SAML:2.0:bindings:HTTP-POST";
        }
    } else {
        LOGGER.log(Level.INFO, "SAML2 HTTP Binding not found in request. Defaulting to HTTP-POST");
        httpBinding = "urn:oasis:names:tc:SAML:2.0:bindings:HTTP-POST";
    }
    SSOAgentConfig config = (SSOAgentConfig) filterConfig.getServletContext().
            getAttribute(SSOAgentConstants.CONFIG_BEAN_NAME);
    config.getSAML2().setHttpBinding(httpBinding);
    config.getOpenId().setClaimedId(servletRequest.getParameter(
            SSOAgentConstants.SSOAgentConfig.OpenID.CLAIMED_ID));
    config.getOpenId().setMode(servletRequest.getParameter(
            SSOAgentConstants.OpenID.OPENID_MODE));

    if (StringUtils.isNotEmpty(servletRequest.getParameter(USERNAME)) &&
            StringUtils.isNotEmpty(servletRequest.getParameter(PASSWORD))) {

        String authorization = servletRequest.getParameter(USERNAME) + ":" + servletRequest.getParameter(PASSWORD);
        // Base64 encoded username:password value
        authorization = Base64.encode(authorization.getBytes(CHARACTER_ENCODING));
        String htmlPayload = "<html>\n" +
                "<body>\n" +
                "<p>You are now redirected back to " + properties.getProperty("SAML2.IdPURL") + " \n" +
                "If the redirection fails, please click the post button.</p>\n" +
                "<form method='post' action='" + properties.getProperty("SAML2.IdPURL") + "'>\n" +
                "<input type='hidden' name='sectoken' value='" + authorization + "'/>\n" +
                "<p>\n" +
                "<!--$saml_params-->\n" +
                "<button type='submit'>POST</button>\n" +
                "</p>\n" +
                "</form>\n" +
                "<script type='text/javascript'>\n" +
                "document.forms[0].submit();\n" +
                "</script>\n" +
                "</body>\n" +
                "</html>";
        config.getSAML2().setPostBindingRequestHTMLPayload(htmlPayload);
    } else {
        // Reset previously sent HTML payload
        config.getSAML2().setPostBindingRequestHTMLPayload(null);
    }
    servletRequest.setAttribute(SSOAgentConstants.CONFIG_BEAN_NAME, config);
    super.doFilter(servletRequest, servletResponse, filterChain);
}
 
Example 19
Source File: CryptoUtil.java    From micro-integrator with Apache License 2.0 2 votes vote down vote up
/**
 * Encrypt the given plain text and base64 encode the encrypted content.
 *
 * @param plainText The plaintext value to be encrypted and base64
 *                  encoded
 * @return The base64 encoded cipher text
 * @throws org.wso2.micro.core.util.CryptoException On error during encryption
 */
public String encryptAndBase64Encode(byte[] plainText) throws org.wso2.micro.core.util.CryptoException {
    return Base64.encode(encrypt(plainText));
}
 
Example 20
Source File: UserStoreConfigXMLProcessor.java    From micro-integrator with Apache License 2.0 2 votes vote down vote up
/**
 * Function to base64 encode ciphertext and set ciphertext
 * @param cipher
 */
public void setCipherBase64Encoded(byte[] cipher) {
    this.c = Base64.encode(cipher);
}