-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVizHashPanel.java
More file actions
231 lines (203 loc) · 7.32 KB
/
VizHashPanel.java
File metadata and controls
231 lines (203 loc) · 7.32 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
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
/* Copyright 2011-2013 Edouard Garnier de Labareyre
*
* This file is part of ViszHash4j
* Visual Hash implementation in java
* This is free software under the zlib/libpng licence
* http://www.opensource.org/licenses/zlib-license.php
*
* Widely inspired from VizHash_GD php code (https://github.com/sebsauvage/VizHash)
* Thanks to Sebastien Sauvage for this piece of code
*/
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.security.MessageDigest;
import java.util.ArrayList;
import javax.swing.JPanel;
/**
* JPanel that computes and display the VizHash picture for a given message String
* The obtained picture aims to be similar to those generated by VizHash_GD (https://github.com/sebsauvage/VizHash)
*/
public class VizHashPanel extends JPanel{
private String message_hash="";
private ArrayList<Integer> values = new ArrayList<Integer>();
private int values_index=0;
private int width,height;
public VizHashPanel(){
}
/**
* Display the VizHash shape of a given message
* @param message the message to vizhash
* @return false if a problem is encoutered during computation
*/
public boolean setValue(String message){
try{
//compute hash String
String md5_hash = convertToHex(MessageDigest.getInstance("MD5").digest(message.getBytes("UTF8")));
String sha1_hash = convertToHex(MessageDigest.getInstance("SHA1").digest(message.getBytes("UTF8")));
message_hash=sha1_hash+md5_hash+new StringBuffer(sha1_hash+md5_hash).reverse().toString();
//build int values array
values = new ArrayList<Integer>();
values_index=0;
for(int i=0; i<message_hash.length();i+=2){
int dec = Integer.parseInt(message_hash.substring(i,i+2),16);
values.add(dec);
}
//paint component with this new value
repaint();
}catch(Exception e){
System.out.println("Error while hashing string \""+message+"\":");
e.printStackTrace();
return false;
}
return true;
}
@Override
public Dimension getPreferredSize() {
return new Dimension(256,256);
}
@Override
public void paintComponent(Graphics g) {
values_index=0;
width=this.getWidth();
height=this.getHeight();
Graphics2D g2 = (Graphics2D)g;
RenderingHints rh = new RenderingHints(
RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
g2.setRenderingHints(rh);
super.paintComponent(g);
int red0 = getInt();
int green0 = getInt();
int blue0 = getInt();
int red=red0;
int green=green0;
int blue=blue0;
Color bg0 = new Color(red0,green0,blue0);
//paint horizontal or vertical gradient in background
GradientPaint bg = new GradientPaint(0,0,bg0,0, height,Color.BLACK);
if(getInt()%2==0){
bg = new GradientPaint(0,0,bg0,width,0,Color.BLACK);
}
g2.setPaint(bg);
g2.fillRect(0, 0, width,height);
//draw 7 or less shapes, in a similar color theme than background
int action;
Color color;
for(int i=0;i<7;i++){
action = getInt();
color = new Color(red,green,blue);
red=(red0+getInt()/25)%256;
green=(green0+getInt()/25)%256;
blue=(blue0+getInt()/25)%256;
red0=red;
green0=green;
blue0=blue;
drawShape(g,action,color);
}
//draw the last shape with a color than can be new
color = new Color(getInt(),getInt(),getInt());
drawShape(g,getInt(),color);
}
/**
* This function paints a filled shape which can be a rectangle, an ellipse, a polygon or an arc
* @param g the graphic where to paint
* @param action an integer between 0 and 255
* @param color the color of the shape to paint
*/
public void drawShape(Graphics g,int action,Color color){
int w,h;
g.setColor(color);
switch(action%7){
case 0:
//rectangle
int[] r = {getXCoordinate(),getYCoordinate(),getXCoordinate(),getYCoordinate()};//order of call of getInt() is important to stay the same as VizHash_GD
int x0=Math.min(r[0],r[2]);
int y0=Math.min(r[1],r[3]);
int x1=Math.max(r[0],r[2]);
int y1=Math.max(r[1],r[3]);
g.fillRect(x0,y0,x1-x0,y1-y0);
break;
case 1:
case 2:
//ellipse
int xc=getXCoordinate();
int yc=getYCoordinate();
w=getXCoordinate();
h=getYCoordinate();
g.fillOval(xc-(w/2),yc-(h/2),w,h);
break;
case 3:
//polygon
int[] p = {getXCoordinate(),getYCoordinate(),
getXCoordinate(),getYCoordinate(),
getXCoordinate(),getYCoordinate(),
getXCoordinate(),getYCoordinate()};
int[] xPoints={p[0],p[2],p[4],p[6]};
int[] yPoints={p[1],p[3],p[5],p[7]};
g.fillPolygon(xPoints, yPoints, 4);
break;
case 4:
case 5:
case 6:
//arc
int as=getInt()*360/256;
int ae=as+((getInt()*180)/256);
int start=-as;
int angle=as-ae;
int cx=getXCoordinate();
int cy=getYCoordinate();
w=getXCoordinate();
h=getYCoordinate();
g.fillArc(cx-(w/2), cy-(h/2), w, h, start, angle);
break;
}
}
/**
* This function returns the next int from the values array
* @return an int from the values array
*/
private int getInt(){
int v = values.get(values_index);
values_index++;
values_index%=values.size();
return v;
}
/**
* Same as getInt but normalized to component width
* @return int
*/
private int getXCoordinate(){
return width*getInt()/256;
}
/**
* Same as getInt but normalized to component height
* @return int
*/
private int getYCoordinate(){
return height*getInt()/256;
}
/**
* This function converts the byte array in hexadecimal String
* (snippet found on pastebin http://pastebin.com/air8WKN5)
* @param data the byte array to convert
* @return
*/
private static String convertToHex(byte[] data) {
StringBuilder buf = new StringBuilder();
for (int i = 0; i < data.length; i++) {
int halfbyte = (data[i] >>> 4) & 0x0F;
int two_halfs = 0;
do {
if ((0 <= halfbyte) && (halfbyte <= 9))
buf.append((char) ('0' + halfbyte));
else
buf.append((char) ('a' + (halfbyte - 10)));
halfbyte = data[i] & 0x0F;
} while(two_halfs++ < 1);
}
return buf.toString();
}
}