Search in sources :

Example 11 with Base64.encodeBase64String

use of org.apache.commons.codec.binary.Base64.encodeBase64String in project hadoop by apache.

the class Signer method computeSignature.

/**
   * Returns then signature of a string.
   *
   * @param secret The secret to use
   * @param str string to sign.
   *
   * @return the signature for the string.
   */
protected String computeSignature(byte[] secret, String str) {
    try {
        MessageDigest md = MessageDigest.getInstance("SHA");
        md.update(str.getBytes(Charset.forName("UTF-8")));
        md.update(secret);
        byte[] digest = md.digest();
        return new Base64(0).encodeToString(digest);
    } catch (NoSuchAlgorithmException ex) {
        throw new RuntimeException("It should not happen, " + ex.getMessage(), ex);
    }
}
Also used : Base64(org.apache.commons.codec.binary.Base64) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) MessageDigest(java.security.MessageDigest)

Example 12 with Base64.encodeBase64String

use of org.apache.commons.codec.binary.Base64.encodeBase64String in project camel by apache.

the class SigningProcessor method process.

public void process(Exchange exchange) throws Exception {
    Signature service = initSignatureService(exchange);
    calculateSignature(exchange, service);
    byte[] signature = service.sign();
    Message in = exchange.getIn();
    clearMessageHeaders(in);
    Message out = exchange.getOut();
    out.copyFrom(in);
    out.setHeader(config.getSignatureHeaderName(), new Base64().encode(signature));
}
Also used : Base64(org.apache.commons.codec.binary.Base64) Message(org.apache.camel.Message) Signature(java.security.Signature)

Example 13 with Base64.encodeBase64String

use of org.apache.commons.codec.binary.Base64.encodeBase64String in project amos-ss17-alexa by c-i-ber.

the class SignedRequestsHelper method hmac.

/**
     * Compute the HMAC.
     *  
     * @param stringToSign  String to compute the HMAC over.
     * @return              base64-encoded hmac value.
     */
private String hmac(String stringToSign) {
    String signature = null;
    byte[] data;
    byte[] rawHmac;
    try {
        data = stringToSign.getBytes(UTF8_CHARSET);
        rawHmac = mac.doFinal(data);
        Base64 encoder = new Base64();
        signature = new String(encoder.encode(rawHmac));
    } catch (UnsupportedEncodingException e) {
        throw new RuntimeException(UTF8_CHARSET + " is unsupported!", e);
    }
    return signature;
}
Also used : Base64(org.apache.commons.codec.binary.Base64) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Example 14 with Base64.encodeBase64String

use of org.apache.commons.codec.binary.Base64.encodeBase64String in project nem2-sdk-java by nemtech.

the class Base64Encoder method getBytes.

/**
 * Converts a string to a byte array.
 *
 * @param base64String The input Base64 string.
 * @return The output byte array.
 */
public static byte[] getBytes(final String base64String) {
    final Base64 codec = new Base64();
    final byte[] encodedBytes = StringEncoder.getBytes(base64String);
    if (!codec.isInAlphabet(encodedBytes, true)) {
        throw new IllegalArgumentException("malformed base64 string passed to getBytes");
    }
    return codec.decode(encodedBytes);
}
Also used : Base64(org.apache.commons.codec.binary.Base64)

Example 15 with Base64.encodeBase64String

use of org.apache.commons.codec.binary.Base64.encodeBase64String in project tmdm-studio-se by Talend.

the class PasswordUtil method decryptPasswordBase64.

public static String decryptPasswordBase64(String encodedPassword) {
    Base64 base64 = new Base64();
    byte[] debytes = null;
    String decodeStr = null;
    debytes = base64.decode(encodedPassword.getBytes());
    decodeStr = new String(debytes);
    return decodeStr;
}
Also used : Base64(org.apache.commons.codec.binary.Base64)

Aggregations

Base64 (org.apache.commons.codec.binary.Base64)149 IOException (java.io.IOException)33 Test (org.junit.Test)29 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)14 Base64.encodeBase64String (org.apache.commons.codec.binary.Base64.encodeBase64String)14 ByteArrayInputStream (java.io.ByteArrayInputStream)11 InputStream (java.io.InputStream)11 HttpServletRequest (javax.servlet.http.HttpServletRequest)11 HttpServletResponse (javax.servlet.http.HttpServletResponse)11 SecretKeySpec (javax.crypto.spec.SecretKeySpec)9 URL (java.net.URL)8 HashMap (java.util.HashMap)8 Cipher (javax.crypto.Cipher)8 File (java.io.File)7 MessageDigest (java.security.MessageDigest)7 Mac (javax.crypto.Mac)7 ServletException (javax.servlet.ServletException)7 FileNotFoundException (java.io.FileNotFoundException)5 UnsupportedEncodingException (java.io.UnsupportedEncodingException)5 URLConnection (java.net.URLConnection)5