public String handleResource(HtmlResource resource) throws IOException { char[] base64Data = new char[0]; byte[] data = resource.getData(); if (data != null) base64Data = Base64Coder.encode(data); return String.format("data:%s;base64,%s", resource.getMimeType(), new String(base64Data)); }
public String encrypt(String str) throws Exception { // return SimpleCrypto.encrypt(this.key, str); // Encode the string into bytes using utf-8 byte[] utf8 = str.getBytes("UTF8"); // Encrypt byte[] enc = ecipher.doFinal(utf8); // Encode bytes to base64 to get a string return Base64Coder.encode(enc); }
public static String sign(String s, String privkey) { String r = null; PrivateKey myprikey = null; byte[] bs = Base64Coder.decode(privkey.toCharArray()); PKCS8EncodedKeySpec spec = new PKCS8EncodedKeySpec(bs); try { myprikey = KeyFactory.getInstance("DSA").generatePrivate(spec); Signature signet = Signature.getInstance("DSA"); signet.initSign(myprikey); byte[] b = s.getBytes(); signet.update(b, 0, b.length); byte[] signed = signet.sign(); r = new String(Base64Coder.encode(signed)); } catch (Throwable e) { } return r; }
// Test Base64Coder against sun.misc.BASE64Encoder/Decoder with // random strings. private static void test2() throws Exception { System.out.println("test2 started"); sun.misc.BASE64Encoder enc = new sun.misc.BASE64Encoder(); sun.misc.BASE64Decoder dec = new sun.misc.BASE64Decoder(); java.util.Random rnd = new java.util.Random(0x538afb92); for (int i = 0; i < 50000; i++) { int len = rnd.nextInt(55); byte[] b0 = new byte[len]; rnd.nextBytes(b0); String e1 = new String(Base64Coder.encode(b0)); String e2 = enc.encode(b0); if (!e1.equals(e2)) System.out.println( "Error\ne1=" + e1 + " len=" + e1.length() + "\ne2=" + e2 + " len=" + e2.length()); byte[] b1 = Base64Coder.decode(e1); byte[] b2 = dec.decodeBuffer(e2); if (!compareByteArrays(b1, b0) || !compareByteArrays(b2, b0)) System.out.println("Decoded data not equal. len1=" + b1.length + " len2=" + b2.length); } System.out.println("test2 completed"); }
@Test public void encodeTwoNegativeBytes() { assertEquals("//8=", Base64Coder.encode(new byte[] {-1, -1})); }
@Test public void encodeOneNegativeByte() { assertEquals("/w==", Base64Coder.encode(new byte[] {-1})); }
@Test public void encodeLenIs2Mod3() { assertEquals("QnJhaW4=", Base64Coder.encode(new byte[] {'B', 'r', 'a', 'i', 'n'})); }
@Test public void encodeLenIs1Mod3() { assertEquals("Q2F0cw==", Base64Coder.encode(new byte[] {'C', 'a', 't', 's'})); }
/** * Call method with optional parameters. This is general method. If you want to call your method * with 0-8 parameters, you can use more convenience call() methods * * @param method name of method to call * @param params parameters to pass to method (may be null if method has no parameters) * @return deserialized method return value * @throws XMLRPCException */ @SuppressWarnings("unchecked") public Object callEx(String method, Object[] params) throws XMLRPCException { try { // prepare POST body String body = methodCall(method, params); // set POST body HttpEntity entity = new StringEntity(body); postMethod.setEntity(entity); // This code slightly tweaked from the code by erickok in issue #6 // Force preemptive authentication // This makes sure there is an 'Authentication: ' header being send before trying and failing // and retrying // by the basic authentication mechanism of DefaultHttpClient if (this.httpPreAuth == true) { String auth = this.username + ":" + this.password; postMethod.addHeader( "Authorization", "Basic " + Base64Coder.encode(auth.getBytes()).toString()); } // Log.d(Tag.LOG, "ros HTTP POST"); // execute HTTP POST request HttpResponse response = client.execute(postMethod); // Log.d(Tag.LOG, "ros HTTP POSTed"); // check status code int statusCode = response.getStatusLine().getStatusCode(); // Log.d(Tag.LOG, "ros status code:" + statusCode); if (statusCode != HttpStatus.SC_OK) { throw new XMLRPCException("HTTP status code: " + statusCode + " != " + HttpStatus.SC_OK); } // parse response stuff // // setup pull parser XmlPullParser pullParser = XmlPullParserFactory.newInstance().newPullParser(); entity = response.getEntity(); Reader reader = new InputStreamReader(new BufferedInputStream(entity.getContent())); // for testing purposes only // reader = new StringReader("<?xml // version='1.0'?><methodResponse><params><param><value>\n\n\n</value></param></params></methodResponse>"); pullParser.setInput(reader); // lets start pulling... pullParser.nextTag(); pullParser.require(XmlPullParser.START_TAG, null, Tag.METHOD_RESPONSE); pullParser.nextTag(); // either Tag.PARAMS (<params>) or Tag.FAULT (<fault>) String tag = pullParser.getName(); if (tag.equals(Tag.PARAMS)) { // normal response pullParser.nextTag(); // Tag.PARAM (<param>) pullParser.require(XmlPullParser.START_TAG, null, Tag.PARAM); pullParser.nextTag(); // Tag.VALUE (<value>) // no parser.require() here since its called in XMLRPCSerializer.deserialize() below // deserialize result Object obj = iXMLRPCSerializer.deserialize(pullParser); entity.consumeContent(); return obj; } else if (tag.equals(Tag.FAULT)) { // fault response pullParser.nextTag(); // Tag.VALUE (<value>) // no parser.require() here since its called in XMLRPCSerializer.deserialize() below // deserialize fault result Map<String, Object> map = (Map<String, Object>) iXMLRPCSerializer.deserialize(pullParser); String faultString = (String) map.get(Tag.FAULT_STRING); int faultCode = (Integer) map.get(Tag.FAULT_CODE); entity.consumeContent(); throw new XMLRPCFault(faultString, faultCode); } else { entity.consumeContent(); throw new XMLRPCException( "Bad tag <" + tag + "> in XMLRPC response - neither <params> nor <fault>"); } } catch (XMLRPCException e) { // catch & propagate XMLRPCException/XMLRPCFault throw e; } catch (Exception e) { e.printStackTrace(); // wrap any other Exception(s) around XMLRPCException throw new XMLRPCException(e); } }