Child: [r22] (diff)

Download this file

SpamSumSignature.java    149 lines (124 with data), 4.0 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
package ssdeep;
public class SpamSumSignature
{
/*****************************************************
* FIELDS
*****************************************************/
private /*uint*/long blockSize;
private byte[] hash1;
private byte[] hash2;
/*****************************************************
* UTILS
*****************************************************/
/**
* <p>
* Change a string into an array of bytes
* </p>
*/
public static byte[] GetBytes(String str)
{
byte[] r = new byte[str.length()];
for (int i=0; i<r.length; i++)
r[i] = (byte)str.charAt(i);
return r;
}
/**
* <p>
* Change a string into an array of bytes
* </p>
*/
public static String GetString(byte[] hsh)
{
String r = "";
for (int i=0; i<hsh.length; i++)
r += (char) hsh[i];
return r;
}
/*****************************************************
* CONSTRUCTOR
*****************************************************/
/**
* <p>
* Initializes a new instance of the {@code SpamSumSignature} class.
* </p>
* @param signature The signature.
*/
public SpamSumSignature(String signature)
{
if (null == signature)
throw new IllegalArgumentException("Signature string cannot be null or empty." + "\r\nParameter name: " + "signature");
int idx1 = signature.indexOf(':');
int idx2 = signature.indexOf(':', idx1 + 1);
if (idx1 < 0)
throw new IllegalArgumentException("Signature is not valid." + "\r\nParameter name: " + "signature");
if (idx2 < 0)
throw new IllegalArgumentException("Signature is not valid." + "\r\nParameter name: " + "signature");
blockSize = Integer.parseInt( signature.substring((0), (0) + (idx1)));
hash1 = GetBytes(signature.substring(idx1 + 1, idx1 + 1 + idx2 - idx1 - 1));
hash2 = GetBytes(signature.substring(idx2 + 1));
}
public SpamSumSignature(long blockSize, byte[] hash1, byte[] hash2)
{
this.blockSize = blockSize;
this.hash1 = hash1;
this.hash2 = hash2;
}
/*****************************************************
* METHODS
*****************************************************/
@Override
public boolean equals(Object obj)
{
if (!(obj instanceof SpamSumSignature))
return false;
return this.equals((SpamSumSignature)obj);
}
public boolean equals(SpamSumSignature other)
{
if (this.blockSize != other.blockSize)
return false;
if (this.hash1.length != other.hash1.length)
return false;
if (this.hash2.length != other.hash2.length)
return false;
for (int idx = 0; idx < hash1.length; idx++)
{
if (this.hash1[idx] != other.hash1[idx])
return false;
}
for (int idx = 0; idx < hash2.length; idx++)
{
if (this.hash2[idx] != other.hash2[idx])
return false;
}
return true;
}
@Override
public String toString()
{
String hashText1 = GetString(hash1);
String hashText2 = GetString(hash2);
return blockSize + ":" + hashText1 + ":" + hashText2;
}
/*****************************************************
* PROPERTIES
*****************************************************/
/**
* <p>
* Gets the size of the block.
* </p>Value: The size of the block.
*/
public /*uint*/long getBlockSize() { return blockSize; }
/**
* <p>
* Gets the first hash part.
* </p>Value: The first hash part.
*/
public byte[] getHashPart1() { return hash1; }
/**
* <p>
* Gets the second hash part.
* </p>Value: The second hash part.
*/
public byte[] getHashPart2() { return hash2; }
}