File: Dongle.java

package info (click to toggle)
bbmap 39.20%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 26,024 kB
  • sloc: java: 312,743; sh: 18,099; python: 5,247; ansic: 2,074; perl: 96; makefile: 39; xml: 38
file content (83 lines) | stat: -rwxr-xr-x 2,158 bytes parent folder | download | duplicates (2)
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
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
package fun;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import shared.Tools;

public class Dongle {
	
	public static void main(String[] args) {
		long millis=System.currentTimeMillis();
		if(args.length>0) {
			millis=parse(args[0]);
		}
		long a=encode(millis);
		System.err.println(a);
		System.err.println(millis);
		System.err.println(decode(a));
	}
	
	public static final boolean check(Object...args) {
		long a=min, b=max;
		if(args!=null && args.length>1) {a=(Long)args[1];}
		if(args!=null && args.length>2) {b=(Long)args[2];}
		a=decode(a);
		b=decode(b);
		long x=System.currentTimeMillis();
		return (b>a && b-a<limit && x>=a && x<=b);
	}
	
	private static long encode(long x) {
		x^=number;
		long a=x&mask;
		long b=x&(mask<<1);
		x=a|(Long.rotateLeft(b, rot));
		a=x&mask2;
		b=x&(mask2<<2);
		x=a|Long.rotateRight(b, rot2);
		a=x&mask3;
		b=x&(mask3>>>4);
		x=a|Long.rotateLeft(b, rot3);
		return x;
	}
	
	private static long decode(long x) {
		long a=x&mask3;
		long b=x&(mask3>>>4);
		x=a|(Long.rotateRight(b, rot3));
		a=x&mask2;
		b=x&(mask2<<2);
		x=(a|(Long.rotateLeft(b, rot2)));
		a=x&mask;
		b=x&(mask<<1);
		x=(a|(Long.rotateRight(b, rot)))^number;
		return x;
	}
	
	private static long parse(String s) {
		try {
			if(Tools.isNumeric(s)) {return Long.parseLong(s);}
			Date d=sdf.parse(s);
			System.err.println(d);
			return d.getTime();
		} catch (ParseException e) {
			return -1L;
		}
	}
	
	private static final String pattern="MM-dd-yyyy";
	private static final SimpleDateFormat sdf=new SimpleDateFormat(pattern);
	private static final long min=-6788251374689658131L;
	private static final long max=2715152938288332905L;
	private static final long number=4964420948893066024L;
	private static final long mask=0x5555555555555555L;
	private static final long mask2=0x3333333333333333L;
	private static final long mask3=0xF0F0F0F0F0F0F0F0L;
	private static final long limit=346896001029L;
	private static final int rot=26;
	private static final int rot2=44;
	private static final int rot3=16;
	
}