Example usage for org.apache.commons.codec.binary Base64 encodeBase64String

List of usage examples for org.apache.commons.codec.binary Base64 encodeBase64String

Introduction

In this page you can find the example usage for org.apache.commons.codec.binary Base64 encodeBase64String.

Prototype

public static String encodeBase64String(final byte[] binaryData) 

Source Link

Document

Encodes binary data using the base64 algorithm but does not chunk the output.

Usage

From source file:bobs.mcapisignature.SignatureTest.java

@Test
public void testSignValidHash() throws MCAPIException, SelectCertificateExceprion {
    System.out.println("SignSelectCert");
    byte[] data = "test".getBytes();
    Signature signature = new Signature();
    String Sha1Hash = "6FEE4E5DBB351A252A397F09A50C70587123E824";//CertUtils.getThumbprint(CertUtils.selectCert("dd", "ddd"));
    // Structures.CERT_CONTEXT cert =CertUtils.findCertByHash(Sha1Hash);
    Structures.CERT_CONTEXT cert = CertUtils.selectCert();
    signature.setSignatureAlgorithm("SHA256withRSA");
    signature.setCert(cert);//  w w w. j  a va  2 s.c o  m
    byte[] result = signature.sign(data);
    String resultb64 = Base64.encodeBase64String(result);
    System.out.println(resultb64);
}

From source file:com.mobilehelix.appserver.session.SessionManager.java

public String hashSessionID(byte[] sessionKey) throws AppserverSystemException {
    try {/*  w  ww  .j a v  a2  s .c  o  m*/
        MessageDigest md = MessageDigest.getInstance("SHA-256");
        md.update(sessionKey);
        return Base64.encodeBase64String(md.digest());
    } catch (NoSuchAlgorithmException ex) {
        LOG.log(Level.SEVERE, "Failed to hash session key.", ex);
        throw new AppserverSystemException("Failed to generate session ID hash.", "SessionIDHashingFailed");
    }
}

From source file:com.metamx.milano.proto.MilanoToolTests.java

@Before
public void setUp() throws Exception {
    Descriptors.Descriptor simpleDescriptor = Testing.TestItem.getDescriptor();
    simpleDescriptorProto = simpleDescriptor.toProto();

    simpleFileDescriptor = Testing.getDescriptor();

    simpleFileDescriptorProto = simpleFileDescriptor.toProto();
    DescriptorProtos.FileDescriptorSet.Builder simpleBuilder = DescriptorProtos.FileDescriptorSet.newBuilder();

    for (Descriptors.FileDescriptor fileDescriptor : simpleFileDescriptor.getDependencies()) {
        simpleBuilder.addFile(fileDescriptor.toProto());
    }/*ww  w  .  ja  v a 2  s . com*/
    simpleFileDescriptorSet = simpleBuilder.build();

    simpleTypeMetadata = MilanoTypeMetadata.TypeMetadata.newBuilder().setTypeName(simpleDescriptor.getName())
            .setTypePackageName(simpleDescriptor.getFile().getPackage())
            .setTypeFileDescriptor(simpleDescriptor.getFile().toProto()).build();

    simpleB64Bytes = Base64.encodeBase64String(simpleTypeMetadata.toByteArray());

    Descriptors.Descriptor completeDescriptor = TestingComplete.CompleteTestItem.getDescriptor();
    completeDescriptorProto = completeDescriptor.toProto();

    completeFileDescriptor = TestingComplete.getDescriptor();

    completeFileDescriptorProto = completeFileDescriptor.toProto();
    DescriptorProtos.FileDescriptorSet.Builder completeBuilder = DescriptorProtos.FileDescriptorSet
            .newBuilder();

    for (Descriptors.FileDescriptor fileDescriptor : completeFileDescriptor.getDependencies()) {
        completeBuilder.addFile(fileDescriptor.toProto());
    }
    completeFileDescriptorSet = completeBuilder.build();

    completeTypeMetadata = MilanoTypeMetadata.TypeMetadata.newBuilder()
            .setTypeName(completeDescriptor.getName())
            .setTypePackageName(completeDescriptor.getFile().getPackage())
            .setTypeFileDescriptor(completeDescriptor.getFile().toProto()).build();

    completeB64Bytes = Base64.encodeBase64String(completeTypeMetadata.toByteArray());

}

From source file:biopolis.headless.BiopolisManager.java

public Long[] put(T[] a) throws SQLException, BiopolisGeneralException {
    Long[] ids = new Long[a.length];
    int index = 0;

    for (T x : a) {
        System.out.println(index + "  count  " + a.length);
        String json = (new Gson()).toJson(x);
        System.out.println(json);
        Map<String, Object> map = x.getMap();
        String desci = Base64.encodeBase64String(json.getBytes());
        map.put("desci", desci);
        String queryString = " CREATE (n:" + this.nodetype + " {1} )  RETURN ID(n)";
        Map<String, Object> mm = new HashMap<String, Object>();
        mm.put("1", map);
        try (ResultSet rs = this.pl.getConnNeo4j().executeQuery(queryString, mm)) {
            if (!rs.next()) {
                throw new BiopolisGeneralException("Cannot create " + json);
            } else {
                Long id = rs.getLong(1);
                System.out.println(id);
                ids[index++] = id;//from   ww  w .  ja va 2  s  .co m
            }
            System.out.println("do");
        }
    }
    System.out.println("ok");
    return ids;
}

From source file:com.lixiaocong.rest.TransmissionController.java

@RequestMapping(method = RequestMethod.POST)
public Map<String, Object> post(MultipartFile file) {
    try {//ww w  .java2s .  c om
        String metainfo = Base64.encodeBase64String(file.getBytes());
        if (client.torrentAdd(metainfo))
            return ResponseMsgFactory.createSuccessResponse();
        return ResponseMsgFactory.createFailedResponse("transmission");
    } catch (Exception e) {
        logger.error("transmission ?");
        return ResponseMsgFactory.createFailedResponse("transmission?");
    }
}

From source file:net.jmhertlein.mcanalytics.plugin.daemon.request.PasswordResetRequestHandler.java

@Override
public JSONObject handle(Connection conn, StatementProvider stmts, JSONObject request, ClientMonitor client)
        throws Exception {
    //request params
    String username, oldPass, newPass;
    username = request.getString("username");
    oldPass = request.getString("old");
    newPass = request.getString("new");

    //convert params to bytes
    byte[] oldBytes = oldPass.getBytes("UTF-8"), newBytes = newPass.getBytes("UTF-8");
    byte[] oldHash, salt;

    //retrieve hash and salt from db
    try (PreparedStatement getHashSalt = conn.prepareStatement(stmts.get(SQLString.GET_HASHSALT_FOR_USER))) {
        getHashSalt.setString(1, username);
        try (ResultSet res = getHashSalt.executeQuery()) {
            if (!res.next()) {
                throw new AuthenticationException();
            }//from   ww  w  .  j a v  a2  s .  c o m
            oldHash = Base64.decodeBase64(res.getString("password_hash"));
            salt = Base64.decodeBase64(res.getString("salt"));
        }
    }

    byte[] oldHashComputed = SSLUtil.hash(oldBytes, salt);

    if (!Arrays.equals(oldHash, oldHashComputed)) {
        throw new AuthenticationException();
    }

    byte[] newSalt = SSLUtil.newSalt();
    byte[] newHash = SSLUtil.hash(newBytes, newSalt);

    try (PreparedStatement updateHash = conn.prepareStatement(stmts.get(SQLString.UPDATE_PLAYER_PASSWORD))) {
        updateHash.setString(1, Base64.encodeBase64String(newHash));
        updateHash.setString(2, Base64.encodeBase64String(newSalt));
        updateHash.setString(3, username);
        updateHash.executeUpdate();
    }

    return new JSONObject();
}

From source file:com.bamboocloud.im.provisioner.json.crypto.simple.SimpleEncryptor.java

/**
 * Encrypts with a symmetric cipher./*from  w w w  .  j  av a2  s .c o m*/
 *
 * @param value the value to be encrypted.
 * @return the encrypted value.
 * @throws GeneralSecurityException if a cryptographic operation failed.
 * @throws IOException if an I/O exception occurred.
 */
private Object symmetric(Object object) throws GeneralSecurityException, IOException {
    Cipher symmetric = Cipher.getInstance(cipher);
    symmetric.init(Cipher.ENCRYPT_MODE, key);
    String data = Base64.encodeBase64String(symmetric.doFinal(mapper.writeValueAsBytes(object)));
    byte[] iv = symmetric.getIV();
    HashMap<String, Object> result = new HashMap<String, Object>();
    result.put("cipher", this.cipher);
    result.put("key", this.alias);
    result.put("data", data);
    if (iv != null) {
        result.put("iv", Base64.encodeBase64String(iv));
    }
    return result;
}

From source file:com.splicemachine.derby.stream.output.direct.DirectTableWriterBuilder.java

public String base64Encode() {
    return Base64.encodeBase64String(SerializationUtils.serialize(this));
}

From source file:eu.europa.esig.dss.FileDocument.java

@Override
public String getDigest(final DigestAlgorithm digestAlgorithm) {
    final InputStream inputStream = openStream();
    final byte[] digestBytes = DSSUtils.digest(digestAlgorithm, inputStream);
    IOUtils.closeQuietly(inputStream);/*from   w w w. j  a  va  2s .c  o  m*/
    final String base64Encode = Base64.encodeBase64String(digestBytes);
    return base64Encode;
}

From source file:com.consol.citrus.ws.message.SoapAttachmentTest.java

@Test
public void testFromBinaryAttachment() throws Exception {
    reset(attachment);//ww w .j a va  2 s .c  o  m

    expect(attachment.getContentId()).andReturn("img").once();
    expect(attachment.getContentType()).andReturn("application/octet-stream").times(2);

    expect(attachment.getDataHandler()).andReturn(new DataHandler(
            new StaticTextDataSource("This is img text content!", "application/octet-stream", "UTF-8", "img")));

    replay(attachment);

    SoapAttachment soapAttachment = SoapAttachment.from(attachment);

    Assert.assertEquals(soapAttachment.getContentId(), "img");
    Assert.assertEquals(soapAttachment.getContentType(), "application/octet-stream");
    Assert.assertEquals(soapAttachment.getContent(),
            Base64.encodeBase64String("This is img text content!".getBytes(Charset.forName("UTF-8"))));
    Assert.assertEquals(soapAttachment.getCharsetName(), Charset.defaultCharset().displayName());
    Assert.assertNotNull(soapAttachment.getDataHandler());
    Assert.assertEquals(soapAttachment.getSize(), 25L);

    soapAttachment.setEncodingType(SoapAttachment.ENCODING_BASE64_BINARY);
    Assert.assertEquals(soapAttachment.getContent(),
            Base64.encodeBase64String("This is img text content!".getBytes(Charset.forName("UTF-8"))));

    soapAttachment.setEncodingType(SoapAttachment.ENCODING_HEX_BINARY);
    Assert.assertEquals(soapAttachment.getContent(),
            Hex.encodeHexString("This is img text content!".getBytes(Charset.forName("UTF-8"))).toUpperCase());

    verify(attachment);
}