| 1 | package fun; |
| 2 | |
| 3 | import java.text.ParseException; |
| 4 | import java.text.SimpleDateFormat; |
| 5 | import java.util.Date; |
| 6 | |
| 7 | import shared.Tools; |
| 8 | |
| 9 | public class Dongle { |
| 10 | |
| 11 | public static void main(String[] args) { |
| 12 | long millis=System.currentTimeMillis(); |
| 13 | if(args.length>0) { |
| 14 | millis=parse(args[0]); |
| 15 | } |
| 16 | long a=encode(millis); |
| 17 | System.err.println(a); |
| 18 | System.err.println(millis); |
| 19 | System.err.println(decode(a)); |
| 20 | } |
| 21 | |
| 22 | public static final boolean check(Object...args) { |
| 23 | long a=min, b=max; |
| 24 | if(args!=null && args.length>1) {a=(Long)args[1];} |
| 25 | if(args!=null && args.length>2) {b=(Long)args[2];} |
| 26 | a=decode(a); |
| 27 | b=decode(b); |
| 28 | long x=System.currentTimeMillis(); |
| 29 | return (b>a && b-a<limit && x>=a && x<=b); |
| 30 | } |
| 31 | |
| 32 | private static long encode(long x) { |
| 33 | x^=number; |
| 34 | long a=x&mask; |
| 35 | long b=x&(mask<<1); |
| 36 | x=a|(Long.rotateLeft(b, rot)); |
| 37 | a=x&mask2; |
| 38 | b=x&(mask2<<2); |
| 39 | x=a|Long.rotateRight(b, rot2); |
| 40 | a=x&mask3; |
| 41 | b=x&(mask3>>>4); |
| 42 | x=a|Long.rotateLeft(b, rot3); |
| 43 | return x; |
| 44 | } |
| 45 | |
| 46 | private static long decode(long x) { |
| 47 | long a=x&mask3; |
| 48 | long b=x&(mask3>>>4); |
| 49 | x=a|(Long.rotateRight(b, rot3)); |
| 50 | a=x&mask2; |
| 51 | b=x&(mask2<<2); |
| 52 | x=(a|(Long.rotateLeft(b, rot2))); |
| 53 | a=x&mask; |
| 54 | b=x&(mask<<1); |
| 55 | x=(a|(Long.rotateRight(b, rot)))^number; |
| 56 | return x; |
| 57 | } |
| 58 | |
| 59 | private static long parse(String s) { |
| 60 | try { |
| 61 | if(Tools.isNumeric(s)) {return Long.parseLong(s);} |
| 62 | Date d=sdf.parse(s); |
| 63 | System.err.println(d); |
| 64 | return d.getTime(); |
| 65 | } catch (ParseException e) { |
| 66 | return -1L; |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | private static final String pattern="MM-dd-yyyy"; |
| 71 | private static final SimpleDateFormat sdf=new SimpleDateFormat(pattern); |
| 72 | private static final long min=-6788251374689658131L; |
| 73 | private static final long max=2715152938288332905L; |
| 74 | private static final long number=4964420948893066024L; |
| 75 | private static final long mask=0x5555555555555555L; |
| 76 | private static final long mask2=0x3333333333333333L; |
| 77 | private static final long mask3=0xF0F0F0F0F0F0F0F0L; |
| 78 | private static final long limit=346896001029L; |
| 79 | private static final int rot=26; |
| 80 | private static final int rot2=44; |
| 81 | private static final int rot3=16; |
| 82 | |
| 83 | } |