forked from Anujraval24/Base64Encoding
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBase64Test.java
More file actions
94 lines (80 loc) · 3.02 KB
/
Copy pathBase64Test.java
File metadata and controls
94 lines (80 loc) · 3.02 KB
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
84
85
86
87
88
89
90
91
92
93
94
import java.io.ByteArrayOutputStream;
import java.util.Random;
import java.util.Arrays;
import javax.xml.bind.DatatypeConverter;
public class Base64Test
{
public static void print(byte[] bytes)
{
for (byte b : bytes) {
System.out.printf("%02X ", b);
}
System.out.println();
}
public static void main(String[] args)
{
int steps = 1000000;
Random rand = new Random(System.currentTimeMillis());
System.out.println("Encode, decode");
for (int count = 0; count < steps; count++) {
int len = rand.nextInt(100)+1;
byte[] original = new byte[len];
for (int i = 0; i < len; i++) {
original[i] = (byte)rand.nextInt(0xFF);
}
String encoded = Base64.encode(original);
byte[] decoded = Base64.decode(encoded);
if (!Arrays.equals(original, decoded)) {
print(original);
print(decoded);
System.out.println();
}
}
System.out.println("Encode, decode with other");
for (int count = 0; count < steps; count++) {
int len = rand.nextInt(100)+1;
byte[] original = new byte[len];
for (int i = 0; i < len; i++) {
original[i] = (byte)rand.nextInt(0xFF);
}
String encoded = Base64.encode(original);
byte[] decoded = DatatypeConverter.parseBase64Binary(encoded);
if (!Arrays.equals(original, decoded)) {
print(original);
print(decoded);
System.out.println();
}
}
System.out.println("Encode with other, decode");
for (int count = 0; count < steps; count++) {
int len = rand.nextInt(100)+1;
byte[] original = new byte[len];
for (int i = 0; i < len; i++) {
original[i] = (byte)rand.nextInt(0xFF);
}
String encoded = DatatypeConverter.printBase64Binary(original);
byte[] decoded = Base64.decode(encoded);
if (!Arrays.equals(original, decoded)) {
print(original);
print(decoded);
System.out.println();
}
}
System.out.println("Comparison test");
for (int count = 0; count < 100000; count++) {
int len = rand.nextInt(100)+1;
byte[] original = new byte[len];
for (int i = 0; i < len; i++) {
original[i] = (byte)rand.nextInt(0xFF);
}
String encoded = DatatypeConverter.printBase64Binary(original);
String encoded2 = Base64.encode(original);
if (!encoded.equals(encoded2)) {
System.out.println("mismatch");
System.out.println(encoded);
System.out.println(encoded2);
System.out.println();
}
}
}
}