Java Code Examples for org.apache.commons.codec.binary.Base64#encodeToString()

The following examples show how to use org.apache.commons.codec.binary.Base64#encodeToString() . 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: ZipUtils.java    From beihu-boot with Apache License 2.0 6 votes vote down vote up
/**
 * 使用zip进行压缩
 *
 * @param str 压缩前的文本
 * @return 返回压缩后的文本
 */
public static String zip(String str) {
    if (str == null)
        return null;
    byte[] compressed;
    String compressedStr = null;
    try (ByteArrayOutputStream out = new ByteArrayOutputStream();
         ZipOutputStream zout = new ZipOutputStream(out)) {
        zout.putNextEntry(new ZipEntry("0"));
        zout.write(str.getBytes());
        zout.closeEntry();
        Base64 base64 = new Base64();
        compressedStr = base64.encodeToString(out.toByteArray());
    } catch (IOException e) {
        compressed = null;
    }
    return compressedStr;
}
 
Example 2
Source File: RssFeedServletTest.java    From archiva with Apache License 2.0 6 votes vote down vote up
@Test
public void testRequestNewArtifactsInRepo()
    throws Exception
{
    MockHttpServletRequest request = new MockHttpServletRequest();
    request.setRequestURI( "/feeds/test-repo" );
    request.addHeader( "User-Agent", "Apache Archiva unit test" );
    request.setMethod( "GET" );

    Base64 encoder = new Base64( 0, new byte[0] );
    String userPass = "user1:password1";
    String encodedUserPass = encoder.encodeToString( userPass.getBytes() );
    request.addHeader( "Authorization", "BASIC " + encodedUserPass );

    MockHttpServletResponse mockHttpServletResponse = new MockHttpServletResponse();

    rssFeedServlet.doGet( request, mockHttpServletResponse );

    assertEquals( RssFeedServlet.MIME_TYPE, mockHttpServletResponse.getHeader( "CONTENT-TYPE" ) );
    assertNotNull( "Should have recieved a response", mockHttpServletResponse.getContentAsString() );
    assertEquals( "Should have been an OK response code.", HttpServletResponse.SC_OK,
                  mockHttpServletResponse.getStatus() );

}
 
Example 3
Source File: RssFeedServletTest.java    From archiva with Apache License 2.0 6 votes vote down vote up
@Test
public void testRequestNewVersionsOfArtifact()
    throws Exception
{
    MockHttpServletRequest request = new MockHttpServletRequest();
    request.setRequestURI( "/feeds/org/apache/archiva/artifact-two" );
    request.addHeader( "User-Agent", "Apache Archiva unit test" );
    request.setMethod( "GET" );

    //WebRequest request = new GetMethodWebRequest( "http://localhost/feeds/org/apache/archiva/artifact-two" );

    Base64 encoder = new Base64( 0, new byte[0] );
    String userPass = "user1:password1";
    String encodedUserPass = encoder.encodeToString( userPass.getBytes() );
    request.addHeader( "Authorization", "BASIC " + encodedUserPass );

    MockHttpServletResponse mockHttpServletResponse = new MockHttpServletResponse();

    rssFeedServlet.doGet( request, mockHttpServletResponse );

    assertEquals( RssFeedServlet.MIME_TYPE, mockHttpServletResponse.getHeader( "CONTENT-TYPE" ) );
    assertNotNull( "Should have recieved a response", mockHttpServletResponse.getContentAsString() );
    assertEquals( "Should have been an OK response code.", HttpServletResponse.SC_OK,
                  mockHttpServletResponse.getStatus() );
}
 
Example 4
Source File: BalanzaComprobacionv11.java    From factura-electronica with Apache License 2.0 5 votes vote down vote up
String getSignature(PrivateKey key) throws Exception {
	byte[] bytes = getOriginalBytes();
	Signature sig = Signature.getInstance("SHA1withRSA");
	sig.initSign(key);
	sig.update(bytes);
	byte[] signed = sig.sign();
	Base64 b64 = new Base64(-1);
	return b64.encodeToString(signed);
}
 
Example 5
Source File: BalanzaComprobacionv11.java    From factura-electronica with Apache License 2.0 5 votes vote down vote up
public void sellar(PrivateKey key, X509Certificate cert) throws Exception {
	cert.checkValidity(); 
	String signature = getSignature(key);
	document.setSello(signature);
	byte[] bytes = cert.getEncoded();
	Base64 b64 = new Base64(-1);
	String certStr = b64.encodeToString(bytes);
	document.setCertificado(certStr);
	BigInteger bi = cert.getSerialNumber();
	document.setNoCertificado(new String(bi.toByteArray()));
}
 
Example 6
Source File: CuentasContablesv11.java    From factura-electronica with Apache License 2.0 5 votes vote down vote up
public void sellar(PrivateKey key, X509Certificate cert) throws Exception {
	cert.checkValidity(); 
	String signature = getSignature(key);
	document.setSello(signature);
	byte[] bytes = cert.getEncoded();
	Base64 b64 = new Base64(-1);
	String certStr = b64.encodeToString(bytes);
	document.setCertificado(certStr);
	BigInteger bi = cert.getSerialNumber();
	document.setNoCertificado(new String(bi.toByteArray()));
}
 
Example 7
Source File: PolizasPeriodov11.java    From factura-electronica with Apache License 2.0 5 votes vote down vote up
String getSignature(PrivateKey key) throws Exception {
	byte[] bytes = getOriginalBytes();
	Signature sig = Signature.getInstance("SHA1withRSA");
	sig.initSign(key);
	sig.update(bytes);
	byte[] signed = sig.sign();
	Base64 b64 = new Base64(-1);
	return b64.encodeToString(signed);
}
 
Example 8
Source File: CFDv22.java    From factura-electronica with Apache License 2.0 5 votes vote down vote up
@Override
public void sellar(PrivateKey key, X509Certificate cert) throws Exception {
    String nc = new String(cert.getSerialNumber().toByteArray());
    if (!nc.equals("20001000000200001428")) {
        cert.checkValidity();
    }
    String signature = getSignature(key);
    document.setSello(signature);
    byte[] bytes = cert.getEncoded();
    Base64 b64 = new Base64(-1);
    String certStr = b64.encodeToString(bytes);
    document.setCertificado(certStr);
    document.setNoCertificado(nc);
}
 
Example 9
Source File: TFDv1c32.java    From factura-electronica with Apache License 2.0 5 votes vote down vote up
String getSignature(PrivateKey key) throws Exception {
    byte[] bytes = getOriginalBytes();
    Signature sig = Signature.getInstance("SHA1withRSA");
    sig.initSign(key);
    sig.update(bytes);
    byte[] signed = sig.sign();
    Base64 b64 = new Base64(-1);
    return b64.encodeToString(signed);
}
 
Example 10
Source File: TFDv11c33.java    From factura-electronica with Apache License 2.0 5 votes vote down vote up
String getSignature(PrivateKey key) throws Exception {
    byte[] bytes = getOriginalBytes();
    Signature sig = Signature.getInstance("SHA256withRSA");
    sig.initSign(key);
    sig.update(bytes);
    byte[] signed = sig.sign();
    Base64 b64 = new Base64(-1);
    return b64.encodeToString(signed);
}
 
Example 11
Source File: TFDv1.java    From factura-electronica with Apache License 2.0 5 votes vote down vote up
String getSignature(PrivateKey key) throws Exception {
    byte[] bytes = getOriginalBytes();
    Signature sig = Signature.getInstance("SHA1withRSA");
    sig.initSign(key);
    sig.update(bytes);
    byte[] signed = sig.sign();
    Base64 b64 = new Base64(-1);
    return b64.encodeToString(signed);
}
 
Example 12
Source File: CFDv32.java    From factura-electronica with Apache License 2.0 5 votes vote down vote up
String getSignature(PrivateKey key) throws Exception {
    byte[] bytes = getOriginalBytes();
    Signature sig = Signature.getInstance("SHA1withRSA");
    sig.initSign(key);
    sig.update(bytes);
    byte[] signed = sig.sign();
    Base64 b64 = new Base64(-1);
    return b64.encodeToString(signed);
}
 
Example 13
Source File: PolizasPeriodov11.java    From factura-electronica with Apache License 2.0 5 votes vote down vote up
public void sellar(PrivateKey key, X509Certificate cert) throws Exception {
	cert.checkValidity(); 
	String signature = getSignature(key);
	document.setSello(signature);
	byte[] bytes = cert.getEncoded();
	Base64 b64 = new Base64(-1);
	String certStr = b64.encodeToString(bytes);
	document.setCertificado(certStr);
	BigInteger bi = cert.getSerialNumber();
	document.setNoCertificado(new String(bi.toByteArray()));
}
 
Example 14
Source File: CFDv3.java    From factura-electronica with Apache License 2.0 5 votes vote down vote up
@Override
public void sellar(PrivateKey key, X509Certificate cert) throws Exception {
    String nc = new String(cert.getSerialNumber().toByteArray());
    if (!nc.equals("20001000000200001428")) {
        cert.checkValidity();
    }
    String signature = getSignature(key);
    document.setSello(signature);
    byte[] bytes = cert.getEncoded();
    Base64 b64 = new Base64(-1);
    String certStr = b64.encodeToString(bytes);
    document.setCertificado(certStr);
    document.setNoCertificado(nc);
}
 
Example 15
Source File: CFDv33.java    From factura-electronica with Apache License 2.0 5 votes vote down vote up
String getSignature(PrivateKey key) throws Exception {
    byte[] bytes = getOriginalBytes();
    Signature sig = Signature.getInstance("SHA256withRSA");
    sig.initSign(key);
    sig.update(bytes);
    byte[] signed = sig.sign();
    Base64 b64 = new Base64(-1);
    return b64.encodeToString(signed);
}
 
Example 16
Source File: Token.java    From hadoop with Apache License 2.0 5 votes vote down vote up
/**
 * Generate a string with the url-quoted base64 encoded serialized form
 * of the Writable.
 * @param obj the object to serialize
 * @return the encoded string
 * @throws IOException
 */
private static String encodeWritable(Writable obj) throws IOException {
  DataOutputBuffer buf = new DataOutputBuffer();
  obj.write(buf);
  Base64 encoder = new Base64(0, null, true);
  byte[] raw = new byte[buf.getLength()];
  System.arraycopy(buf.getData(), 0, raw, 0, buf.getLength());
  return encoder.encodeToString(raw);
}
 
Example 17
Source File: UKTools.java    From youkefu with Apache License 2.0 5 votes vote down vote up
public static String encode(Object obj) {
	Base64 base64 = new Base64();
   	try {
		return base64.encodeToString(UKTools.toBytes(obj)) ;
	} catch (Exception e) {
		e.printStackTrace();
	}
   	return null;
}
 
Example 18
Source File: ClusterSpec.java    From TensorFlowOnYARN with Apache License 2.0 4 votes vote down vote up
private String base64Encoded(String json) throws JsonProcessingException {
  byte[] data = json.getBytes();
  Base64 encoder = new Base64(0, null, true);
  return encoder.encodeToString(data);
}
 
Example 19
Source File: ZipUtils.java    From beihu-boot with Apache License 2.0 4 votes vote down vote up
public static String base64Encode(byte[] input)
{

    Base64 base64 = new Base64();
    return base64.encodeToString(input);
}
 
Example 20
Source File: OpenIDConnectAuthenticationTest.java    From java with Apache License 2.0 3 votes vote down vote up
private static String exportCert(X509Certificate cert) throws Exception {

    Base64 encoder = new Base64(64);

    String b64 = encoder.encodeToString(cert.getEncoded());

    b64 = "-----BEGIN CERTIFICATE-----\n" + b64 + "-----END CERTIFICATE-----\n";

    return b64;
  }