下面列出了 org.apache.commons.codec.binary.Base64 # encodeToString ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@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() );
}
/**
* 使用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;
}
@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() );
}
public static String encode(Object obj) {
Base64 base64 = new Base64();
try {
return base64.encodeToString(UKTools.toBytes(obj)) ;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* 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);
}
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);
}
@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);
}
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()));
}
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);
}
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);
}
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);
}
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);
}
@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);
}
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);
}
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()));
}
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()));
}
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);
}
public static String base64Encode(byte[] input)
{
Base64 base64 = new Base64();
return base64.encodeToString(input);
}
private String base64Encoded(String json) throws JsonProcessingException {
byte[] data = json.getBytes();
Base64 encoder = new Base64(0, null, true);
return encoder.encodeToString(data);
}
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;
}