Revisions
-
scizzr revised this gist
Jun 3, 2013 . 1 changed file with 4 additions and 4 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -2,18 +2,18 @@ public class Base64 { private static final String tbl = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; public static byte[] encode(String string) { return encode(string, false); } public static byte[] encode(byte[] data) { return encode(data, false); } public static byte[] encode(String string, boolean doSplit) { //try to use the appropriate encoding string, but fallback to default locale try { return encode(string.getBytes("UTF-8"), doSplit); } catch (Exception ex) { } return encode(string.getBytes(), doSplit); } /** -
scizzr revised this gist
Jun 3, 2013 . 1 changed file with 5 additions and 5 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,15 +1,15 @@ public class Base64 { private static final String tbl = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; public static byte[] encode(String string) { return base64_encode(string, false); } public static byte[] encode(byte[] data) { return base64_encode(data, false); } public static byte[] encode(String string, boolean doSplit) { //try to use the appropriate encoding string, but fallback to default locale try { return base64_encode(string.getBytes("UTF-8"), doSplit); } catch (Exception ex) { } @@ -24,7 +24,7 @@ public static byte[] base64_encode(String string, boolean doSplit) { * @author [John Comeau, Fred Sanchez, Cipher_nemo, Teresa and 9 others]<br/> * Modified from <a href="http://www.wikihow.com/Encode-a-String-to-Base64-With-Java">WikiHow</a> */ public static byte[] encode(byte[] data, boolean doSplit) { String encoded = "", split = ""; //determine how many padding bytes to add to the end int paddingCount = (3 - (data.length % 3)) % 3; @@ -55,7 +55,7 @@ public static byte[] base64_encode(byte[] data, boolean doSplit) { * @author [Unknown author]<br/> * Modified from <a href="http://en.wikibooks.org/wiki/Algorithm_Implementation/Miscellaneous/Base64#Java_2">WikiBooks</a> */ public static byte[] decode(byte[] data) { String decoded = ""; //replace any incoming padding with a zero pad (the 'A' character is zero) String pad = (data[data.length - 1] == '=' ? (data[data.length - 2] == '=' ? "AA" : "A") : ""); -
scizzr revised this gist
Jun 3, 2013 . 1 changed file with 14 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -16,6 +16,14 @@ public static byte[] base64_encode(String string, boolean doSplit) { return base64_encode(string.getBytes(), doSplit); } /** * Base64 encode a byte array * @param data The data to encode * @param doSplit should we split the data into 76-character lines? * @return base64-encoded byte array * @author [John Comeau, Fred Sanchez, Cipher_nemo, Teresa and 9 others]<br/> * Modified from <a href="http://www.wikihow.com/Encode-a-String-to-Base64-With-Java">WikiHow</a> */ public static byte[] base64_encode(byte[] data, boolean doSplit) { String encoded = "", split = ""; //determine how many padding bytes to add to the end @@ -41,6 +49,12 @@ public static byte[] base64_encode(byte[] data, boolean doSplit) { return (doSplit ? split.trim() : encoded).getBytes(); } /** * @param data The data to decode * @return base64-decoded byte array * @author [Unknown author]<br/> * Modified from <a href="http://en.wikibooks.org/wiki/Algorithm_Implementation/Miscellaneous/Base64#Java_2">WikiBooks</a> */ public static byte[] base64_decode(byte[] data) { String decoded = ""; //replace any incoming padding with a zero pad (the 'A' character is zero) -
scizzr revised this gist
Jun 3, 2013 . 1 changed file with 37 additions and 64 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,88 +1,61 @@ public class Base64 { private static final String tbl = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; public static byte[] base64_encode(String string) { return base64_encode(string, false); } public static byte[] base64_encode(byte[] data) { return base64_encode(data, false); } public static byte[] base64_encode(String string, boolean doSplit) { //try to use the appropriate encoding string, but fallback to default locale try { return base64_encode(string.getBytes("UTF-8"), doSplit); } catch (Exception ex) { } return base64_encode(string.getBytes(), doSplit); } public static byte[] base64_encode(byte[] data, boolean doSplit) { String encoded = "", split = ""; //determine how many padding bytes to add to the end int paddingCount = (3 - (data.length % 3)) % 3; //add any necessary padding to the input byte[] padded = new byte[data.length + paddingCount]; System.arraycopy(data, 0, padded, 0, data.length); data = padded; //process 3 bytes at a time, output 4 bytes at a time for (int i = 0; i < data.length; i += 3) { int j = ((data[i] & 0xff) << 16) + ((data[i + 1] & 0xff) << 8) + (data[i + 2] & 0xff); encoded = encoded + tbl.charAt((j >> 18) & 0x3f) + tbl.charAt((j >> 12) & 0x3f) + tbl.charAt((j >> 6) & 0x3f) + tbl.charAt(j & 0x3f); } //replace encoded padding nulls with "=" encoded = encoded.substring(0, encoded.length() - paddingCount) + "==".substring(0, paddingCount); //split into multiple lines if (doSplit) { for (int i = 0; i < encoded.length(); i += 76) { split += encoded.substring(i, Math.min(encoded.length(), i + 76)) + "\r\n"; } } //return the result, cleaning it up with a trim() if needed return (doSplit ? split.trim() : encoded).getBytes(); } public static byte[] base64_decode(byte[] data) { String decoded = ""; //replace any incoming padding with a zero pad (the 'A' character is zero) String pad = (data[data.length - 1] == '=' ? (data[data.length - 2] == '=' ? "AA" : "A") : ""); data = (new String(data).substring(0, data.length - pad.length()) + pad).getBytes(); //increment over the length of this encrypted string, four characters //at a time for (int i = 0; i < data.length; i += 4) { //each of these four characters represents a 6-bit index in the base64 characters list //which, when concatenated, will give the 24-bit number for the original 3 characters int j = (tbl.indexOf(data[i]) << 18) + (tbl.indexOf(data[i+1]) << 12) + (tbl.indexOf(data[i+2]) << 6) + tbl.indexOf(data[i+3]); //split the 24-bit number into the original three 8-bit (ASCII) characters decoded += "" + (char) ((j >>> 16) & 0xFF) + (char) ((j >>> 8) & 0xFF) + (char) (j & 0xFF); } //remove any zero pad that was added to make this a multiple of 24 bits return (decoded.substring(0, decoded.length() - pad.length())).getBytes(); } } -
scizzr revised this gist
Jun 3, 2013 . 1 changed file with 34 additions and 34 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,38 +1,38 @@ public class Base64 { public static String base64_encode(String string) { return base64_encode(string, false); } public static String base64_encode(String string, boolean doSplit) { String tbl = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; String encoded = "", split = ""; byte[] data; //try to use the appropriate encoding string, but fallback to default locale try { data = string.getBytes("UTF-8"); } catch (Exception ex) { data = string.getBytes(); } //determine how many padding bytes to add to the end int paddingCount = (3 - (data.length % 3)) % 3; //add any necessary padding to the input byte[] padded = new byte[data.length + paddingCount]; System.arraycopy(data, 0, padded, 0, data.length); data = padded; //proces 3 bytes at a time, output 4 bytes at a time //worry about CRLF later for (int i = 0; i < data.length; i += 3) { int j = ((data[i] & 0xff) << 16) + ((data[i + 1] & 0xff) << 8) + (data[i + 2] & 0xff); encoded = encoded + tbl.charAt((j >> 18) & 0x3f) + tbl.charAt((j >> 12) & 0x3f) + tbl.charAt((j >> 6) & 0x3f) + tbl.charAt(j & 0x3f); } //replace encoded padding nulls with "=" encoded = encoded.substring(0, encoded.length() - paddingCount) + "==".substring(0, paddingCount); //split into multiple lines for (int i = 0; i < encoded.length(); i += 76) { split += encoded.substring(i, Math.min(encoded.length(), i + 76)) + "\r\n"; } //trim the new line at the end of the string and return the result return doSplit ? split.trim() : encoded; } public static byte[] decode(String data) { int[] tbl = { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -
scizzr revised this gist
Jun 3, 2013 . 1 changed file with 27 additions and 39 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -2,45 +2,33 @@ public class Base64 { public static String encode(byte[] data) { String encoded = ""; String split = ""; String tbl = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; byte[] data; //try to use the appropriate encoding string, but fallback to default locale try { data = string.getBytes("UTF-8"); } catch (Exception ex) { data = string.getBytes(); } //determine how many padding bytes to add to the end int paddingCount = (3 - (data.length % 3)) % 3; //add any necessary padding to the input byte[] padded = new byte[data.length + paddingCount]; System.arraycopy(data, 0, padded, 0, data.length); data = padded; //proces 3 bytes at a time, output 4 bytes at a time //worry about CRLF later for (int i = 0; i < data.length; i += 3) { int j = ((data[i] & 0xff) << 16) + ((data[i + 1] & 0xff) << 8) + (data[i + 2] & 0xff); encoded = encoded + tbl.charAt((j >> 18) & 0x3f) + tbl.charAt((j >> 12) & 0x3f) + tbl.charAt((j >> 6) & 0x3f) + tbl.charAt(j & 0x3f); } //replace encoded padding nulls with "=" encoded = encoded.substring(0, encoded.length() - paddingCount) + "==".substring(0, paddingCount); //split into multiple lines for (int i = 0; i < encoded.length(); i += 76) { split += encoded.substring(i, Math.min(encoded.length(), i + 76)) + "\r\n"; } //trim the new line at the end of the string and return the result return split.trim(); } public static byte[] decode(String data) -
Emil Hernvall revised this gist
May 4, 2011 . 1 changed file with 20 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,20 @@ # script to generate the reverse lookup table s = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/" res = [(ord(c), i) for i,c in enumerate(s)] lookup = dict(res) f = [] for i in xrange(0,255): if lookup.has_key(i): n = str(lookup[i]) if len(n) == 1: n = " " + n f.append(" " + n) else: f.append(" " + str(-1)) print len(f) print ",".join(f) -
Emil Hernvall revised this gist
May 4, 2011 . 1 changed file with 55 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -42,4 +42,59 @@ public static String encode(byte[] data) return buffer.toString(); } public static byte[] decode(String data) { int[] tbl = { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 62, -1, -1, -1, 63, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, -1, -1, -1, -1, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1, -1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 }; byte[] bytes = data.getBytes(); StringBuilder buffer = new StringBuilder(); for (int i = 0; i < bytes.length; ) { int b = 0; if (tbl[bytes[i]] != -1) { b = (tbl[bytes[i]] & 0xFF) << 18; } // skip unknown characters else { i++; continue; } if (i + 1 < bytes.length && tbl[bytes[i+1]] != -1) { b = b | ((tbl[bytes[i+1]] & 0xFF) << 12); } if (i + 2 < bytes.length && tbl[bytes[i+2]] != -1) { b = b | ((tbl[bytes[i+2]] & 0xFF) << 6); } if (i + 3 < bytes.length && tbl[bytes[i+3]] != -1) { b = b | (tbl[bytes[i+3]] & 0xFF); } while ((b & 0xFFFFFF) != 0) { int c = (b & 0xFF0000) >> 16; buffer.append((char)c); b <<= 8; } i += 4; } return buffer.toString().getBytes(); } } -
EmilHernvall created this gist
May 3, 2011 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,45 @@ public class Base64 { public static String encode(byte[] data) { char[] tbl = { 'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P', 'Q','R','S','T','U','V','W','X','Y','Z','a','b','c','d','e','f', 'g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v', 'w','x','y','z','0','1','2','3','4','5','6','7','8','9','+','/' }; StringBuilder buffer = new StringBuilder(); int pad = 0; for (int i = 0; i < data.length; i += 3) { int b = ((data[i] & 0xFF) << 16) & 0xFFFFFF; if (i + 1 < data.length) { b |= (data[i+1] & 0xFF) << 8; } else { pad++; } if (i + 2 < data.length) { b |= (data[i+2] & 0xFF); } else { pad++; } // why the fuck does this have to be here? if (i % 57 == 0 && i > 0) { buffer.append("\n"); } while ((b & 0xFFFFFF) != 0) { int c = (b & 0xFC0000) >> 18; buffer.append(tbl[c]); b <<= 6; } } for (int j = 0; j < pad; j++) { buffer.append("="); } return buffer.toString(); } }