aboutsummaryrefslogtreecommitdiff
path: root/src/Java/miscutil/core/util/Benchmark.java
blob: 1e17175c3b29071462e0cc647a9e78a35f079bd7 (plain)
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
package miscutil.core.util;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Random;

import cpw.mods.fml.common.FMLLog;

@SuppressWarnings("unused")
public class Benchmark {

	public void math() throws ParseException{
		Random r = new Random();

		FMLLog.info("Looking at the stars in the sky");
		
		// generate some random boolean values
		boolean[] booleans = new boolean[10];
		for (int i = 0; i < booleans.length; i++) {
			booleans[i] = r.nextBoolean();
		}

		//FMLLog.info(getSha256(booleans.toString()));

		/*for (boolean b : booleans) {
    	FMLLog.info(b + ", ");
    }*/

		// generate a uniformly distributed int random numbers
		int[] integers = new int[10];
		for (int i = 0; i < integers.length; i++) {
			integers[i] = r.nextInt();
		}

		FMLLog.info(getSha256(integers.toString()));

		/*for (int i : integers) {s
			FMLLog.info(i + ", ");
		}*/

		// generate a uniformly distributed float random numbers
		float[] floats = new float[10];
		for (int i = 0; i < floats.length; i++) {
			floats[i] = r.nextFloat();
		}

		FMLLog.info(getSha256(floats.toString()));

		/*for (float f : floats) {
			FMLLog.info(f + ", ");
		}*/

		// generate a Gaussian normally distributed random numbers
		double[] gaussians = new double[10];
		for (int i = 0; i < gaussians.length; i++) {
			gaussians[i] = r.nextGaussian();
		}

		FMLLog.info(getSha256(gaussians.toString()));
	}
	
	private String dateTime(){
		DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd - HH:mm:ss");		  
		   //get current date time with Calendar()
		   Calendar cal = Calendar.getInstance();
		   return dateFormat.format(cal.getTime());
	}
	
	public String superhash(String a){
		FMLLog.info("Calculating the cost of life & the universe");
		int i = 1;
		String b = a;
		while (i < 3358 && i > 0){
			if (!b.equals(a)){
				b = a;
			}
			getSha256(b);
			a = b;
			try {
				Thread.sleep(2);
			} catch (InterruptedException e) {
				FMLLog.info("Hashbrown order failed");
				e.printStackTrace();
			}
			if (i == 500 || i == 1000 || i == 1500 || i == 2000 || i == 2500 || i == 3000 || i == 3500 || i == 4000 || i == 5000){
				//FMLLog.info("Calculating orbits around the sun: "+i);
			}
			i++;
		}
		return b;
	}

	private String getSha256(String message) {
		if (message == null || message.isEmpty()) {
			return "";
		}
		String chiper = generateString("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890-=!@#$%^&*()_+`~[];',./{}:<>?|'", 32);
		String key = generateString("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890-=!@#$%^&*()_+`~[];',./{}:<>?|'", 16); // key is used to construct a new SHA-256 key/salt

		// Initialize SHA-256
		MessageDigest digest = null;
		try {
			digest = MessageDigest.getInstance("SHA-256");
		} catch (NoSuchAlgorithmException e) {
			System.err.println(e.getMessage());
		}

		// Hashing entered key to construct a new key/salt
		byte[] keyAsSHA256 = digest.digest(key.getBytes());

		// Encoding the message with CBC
		char[] messageAsChars = message.toCharArray();
		messageAsChars[0] ^= keyAsSHA256[0]; // Avoiding buffer underflow
		for (int i = 1; i < messageAsChars.length; i++) {
			messageAsChars[i] ^= messageAsChars[i - 1]; // XOR with previous character
			messageAsChars[i] ^= keyAsSHA256[i % keyAsSHA256.length]; // XOR with keys hash
		}
		// build cipher from the chars
		chiper = new String(messageAsChars);
		String cipher = MD5(chiper);
		return chiper + "|" + cipher;
	}

	public String MD5(String md5) {
		try {
			java.security.MessageDigest md = java.security.MessageDigest.getInstance("MD5");
			byte[] array = md.digest(md5.getBytes());
			StringBuffer sb = new StringBuffer();
			for (int i = 0; i < array.length; ++i) {
				sb.append(Integer.toHexString((array[i] & 0xFF) | 0x100).substring(1,3));
			}
			return sb.toString();
		} catch (java.security.NoSuchAlgorithmException e) {
		}
		return null;
	}

	public static String generateString(String characters, int length)
	{
		Random r = new Random();
		char[] text = new char[length];
		for (int i = 0; i < length; i++)
		{
			text[i] = characters.charAt(r.nextInt(characters.length()));
		}
		return new String(text);
	}

}