Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.likella.util
- import java.math.BigInteger
- public class BinaryCode {
- protected ArrayList<Byte> code
- static ArrayList<Byte> hexStringToByteArray(String s) {
- ArrayList<Byte> data = []
- for (i in 0..((s.length()-2)/2)) {
- data << ((Character.digit(s.charAt(i*2), 16) << 4 ) + Character.digit(s.charAt(i*2+1), 16))
- }
- data
- }
- static String toHex(ArrayList<Byte> b) {
- String.format("%0" + (b.size() << 1) + "x", new BigInteger(1, (byte[]) b.toArray()))
- }
- BinaryCode(){
- code = []
- }
- BinaryCode(def s, Integer byteSize=null){
- if(s instanceof String)
- code = hexStringToByteArray(s)
- else if(code instanceof BinaryCode)
- code = s.getCode()
- else
- code = s
- if(byteSize){
- if(code.size()<byteSize)
- (code.size()..(byteSize-1)).each{ code<< 0 }
- if(code.size()>byteSize)
- code = code[0..(byteSize-1)]
- }
- println code
- }
- BinaryCode(BinaryCode b){
- code = b.getCode()
- }
- def leftShift(def b){
- def aux = b
- if(b instanceof String)
- aux = b.bytes
- if(b instanceof BinaryCode)
- aux = b.getCode()
- code = [code, aux].flatten()
- this
- }
- def encodeAsMD5(){
- code = hexStringToByteArray( ((byte[])code).encodeAsMD5() )
- this
- }
- def padTo(int n){
- code = Arrays.copyOf( (byte[])code , n)
- this
- }
- String toString(){
- toHex(code)
- }
- ArrayList<Byte> getCode(){
- code
- }
- def xor(def b){
- if(b instanceof String)
- b = b.bytes
- else if (b instanceof BinaryCode)
- b = b.getCode()
- if(size()>0){
- for(i in 0..(size()-1)){
- code[i] = code[i]^b[i]
- }
- }
- this
- }
- int size(){
- code.size()
- }
- }
Add Comment
Please, Sign In to add comment