Example usage for org.bouncycastle.util.encoders Base64 encode

List of usage examples for org.bouncycastle.util.encoders Base64 encode

Introduction

In this page you can find the example usage for org.bouncycastle.util.encoders Base64 encode.

Prototype

public static byte[] encode(byte[] data) 

Source Link

Document

encode the input data producing a base 64 encoded byte array.

Usage

From source file:no.digipost.api.client.filters.request.RequestSignatureFilter.java

License:Apache License

private void setSignatureHeader(final ClientRequestContext request) {
    Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());
    String stringToSign = RequestMessageSignatureUtil
            .getCanonicalRequestRepresentation(new ClientRequestToSign(request));
    log(getClass().getSimpleName() + " beregnet streng som skal signeres:\n===START SIGNATURSTRENG===\n"
            + stringToSign + "===SLUTT SIGNATURSTRENG===");

    byte[] signatureBytes = signer.sign(stringToSign);
    String signature = new String(Base64.encode(signatureBytes));
    request.getHeaders().add(Headers.X_Digipost_Signature, signature);
    log(getClass().getSimpleName() + " satt headeren " + Headers.X_Digipost_Signature + "=" + signature);
}

From source file:no.digipost.api.client.filters.response.ResponseContentSHA256Filter.java

License:Apache License

private void validerBytesMotHashHeader(final String serverHash, final byte[] entityBytes) {
    SHA256Digest digest = new SHA256Digest();

    digest.update(entityBytes, 0, entityBytes.length);
    byte[] result = new byte[digest.getDigestSize()];
    digest.doFinal(result, 0);/*w  ww.j a  v  a2  s  .  co  m*/
    String ourHash = new String(Base64.encode(result));
    if (!serverHash.equals(ourHash)) {
        throw new DigipostClientException(SERVER_SIGNATURE_ERROR,
                "X-Content-SHA256-header matchet ikke innholdet - server-signatur er feil.");
    }
}

From source file:no.digipost.api.useragreements.client.filters.request.RequestSignatureInterceptor.java

License:Apache License

private void setSignatureHeader(final HttpRequest httpRequest) {
    String stringToSign = RequestMessageSignatureUtil
            .getCanonicalRequestRepresentation(new RequestToSign(httpRequest));
    log.debug("String to sign:\n===START SIGNATURSTRENG===\n" + stringToSign + "===SLUTT SIGNATURSTRENG===");

    byte[] signatureBytes = signer.sign(stringToSign);
    String signature = new String(Base64.encode(signatureBytes));
    httpRequest.setHeader(Headers.X_Digipost_Signature, signature);
    log.debug("Settin header " + Headers.X_Digipost_Signature + "=" + signature);
}

From source file:org.apache.cloudstack.server.auth.PBKDF2UserAuthenticator.java

License:Apache License

private static String encode(byte[] input) throws UnsupportedEncodingException {
    return new String(Base64.encode(input), "UTF-8");
}

From source file:org.apache.zeppelin.user.Encryptor.java

License:Apache License

public String encrypt(String inputString) throws IOException {
    byte[] input = inputString.getBytes();
    byte[] result = new byte[encryptCipher.getOutputSize(input.length)];
    int size = encryptCipher.processBytes(input, 0, input.length, result, 0);

    try {//  w  w  w .j  a  v  a  2  s .c  o m
        size += encryptCipher.doFinal(result, size);

        byte[] out = new byte[size];
        System.arraycopy(result, 0, out, 0, size);
        return new String(Base64.encode(out));
    } catch (InvalidCipherTextException e) {
        throw new IOException("Cannot encrypt: " + e.getMessage(), e);
    }
}

From source file:org.bigmouth.nvwa.utils.degist.NativeAesUtils.java

License:Apache License

public static String encryptWith16BitsKey(String input, String key) throws Exception {
    byte[] crypted = null;
    SecretKeySpec skey = new SecretKeySpec(key.getBytes(), "AES");
    Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
    cipher.init(Cipher.ENCRYPT_MODE, skey);
    crypted = cipher.doFinal(input.getBytes());
    return new String(Base64.encode(crypted));
}

From source file:org.bitrepository.protocol.security.BasicSecurityManager.java

License:Open Source License

/**
 * Method to sign a message//from www  .  j a  va 2  s.co  m
 * @param message the message to sign
 * @return the signature for the message, or null if authentication is disabled.
 * @throws MessageSigningException if signing of the message fails.   
 */
public String signMessage(String message) throws MessageSigningException {
    if (repositorySettings.getProtocolSettings().isRequireMessageAuthentication()) {
        try {
            byte[] signature = signer
                    .signMessage(message.getBytes(SecurityModuleConstants.defaultEncodingType));
            return new String(Base64.encode(signature));
        } catch (UnsupportedEncodingException e) {
            throw new SecurityException(SecurityModuleConstants.defaultEncodingType + " encoding not supported",
                    e);
        }
    } else {
        return null;
    }
}

From source file:org.bitrepository.protocol.security.SecurityManagerTest.java

License:Open Source License

@Test(groups = { "regressiontest" })
public void positiveSigningAuthenticationRoundtripTest() throws Exception {
    addDescription("Tests that a roundtrip of signing a request and afterwards authenticating is succedes.");
    addStep("Sign a chunck of data.", "Data is signed succesfully");
    String signature = null;// w w w  . j  a  v a2 s . co m
    try {
        signature = securityManager.signMessage(SecurityTestConstants.getTestData());
    } catch (MessageSigningException e) {
        Assert.fail("Failed signing test data!", e);
    }
    permissionStore.loadPermissions(getSigningCertPermission(), SecurityTestConstants.getComponentID());

    String signatureString = new String(
            Base64.encode(signature.getBytes(SecurityModuleConstants.defaultEncodingType)));
    log.info("Signature for testdata is: " + signatureString);

    addStep("Check signature matches the data ", "Signature and data matches");
    try {
        securityManager.authenticateMessage(SecurityTestConstants.getTestData(), signature);
    } catch (MessageAuthenticationException e) {
        Assert.fail("Failed authenticating test data!", e);
    }
}

From source file:org.bitrepository.protocol.security.SecurityManagerTest.java

License:Open Source License

@Test(groups = { "regressiontest" })
public void negativeSigningAuthenticationRoundtripUnkonwnCertificateTest() throws Exception {
    addDescription("Tests that a roundtrip of signing a request and afterwards authenticating it fails due to "
            + "a unknown certificate.");
    addStep("Sign a chunck of data.", "Data is signed succesfully");
    String signature = null;//from   w w  w  . j  av  a2  s .co  m
    try {
        signature = securityManager.signMessage(SecurityTestConstants.getTestData());
    } catch (MessageSigningException e) {
        Assert.fail("Failed signing test data!", e);
    }
    String signatureString = new String(
            Base64.encode(signature.getBytes(SecurityModuleConstants.defaultEncodingType)));
    log.info("Signature for testdata is: " + signatureString);

    addStep("Check signature matches the data", "Signature cant be matched as certificate is unknown.");
    try {
        securityManager.authenticateMessage(SecurityTestConstants.getTestData(), signature);//signatureString);
        Assert.fail("Authentication did not fail as expected");
    } catch (MessageAuthenticationException e) {
        log.info(e.getMessage());
    }
}

From source file:org.bitrepository.protocol.security.SecurityManagerTest.java

License:Open Source License

@Test(groups = { "regressiontest" })
public void negativeSigningAuthenticationRoundtripBadDataTest() throws Exception {
    addDescription("Tests that a roundtrip of signing a request and afterwards authenticating it fails "
            + "due to bad data");
    addDescription("Tests that a roundtrip of signing a request and afterwards authenticating is succedes.");
    addStep("Sign a chunck of data.", "Data is signed succesfully");
    String signature = null;//from ww w  .j  a v a2s .  c o  m
    try {
        signature = securityManager.signMessage(SecurityTestConstants.getTestData());
    } catch (MessageSigningException e) {
        Assert.fail("Failed signing test data!", e);
    }
    permissionStore.loadPermissions(getSigningCertPermission(), SecurityTestConstants.getComponentID());

    String signatureString = new String(
            Base64.encode(signature.getBytes(SecurityModuleConstants.defaultEncodingType)));
    log.info("Signature for testdata is: " + signatureString);

    addStep("Check signature matches the data ", "Signature and data matches does not match");
    String corruptData = SecurityTestConstants.getTestData() + "foobar";
    try {
        securityManager.authenticateMessage(corruptData, signature);
        Assert.fail("Authentication did not fail as expected!");
    } catch (MessageAuthenticationException e) {
        log.info(e.getMessage());
    }
}