root / DistributedBackupService / src / utils / Utils.java
History | View | Annotate | Download (1.62 KB)
1 | 1 | up20130859 | package utils; |
---|---|---|---|
2 | |||
3 | import java.nio.charset.StandardCharsets; |
||
4 | import java.security.MessageDigest; |
||
5 | import java.security.NoSuchAlgorithmException; |
||
6 | |||
7 | public class Utils { |
||
8 | |||
9 | public static final char CR = (char) 0x0D; |
||
10 | public static final char LF = (char) 0x0A; |
||
11 | |||
12 | public final static String CRLF = "" + CR + LF; |
||
13 | |||
14 | public static final int CHUNK_MAX_SIZE = 64000; |
||
15 | public static final int PACKET_SIZE = 65536; |
||
16 | |||
17 | public enum MessageType { PUTCHUNK, STORED, GETCHUNK, CHUNK, REMOVED, DELETE } |
||
18 | |||
19 | public enum SocketType { MC, MDB, MDR } |
||
20 | |||
21 | private static final String MULTICAST_ADDR = "224.0.0.1"; |
||
22 | private static final int DEFAULT_PORT_MC = 8000; |
||
23 | |||
24 | private static final String MDB_ADDR = "224.0.0.2"; |
||
25 | private static final int DEFAULT_PORT_MDB = 8001; |
||
26 | |||
27 | public static final String FILES = "../files/"; |
||
28 | public static final String CHUNKS = "chunks/"; |
||
29 | |||
30 | public static final String RESTORES = "restores/"; |
||
31 | |||
32 | public static final String VERSION = "1.0"; |
||
33 | public static SocketType MDB; |
||
34 | |||
35 | private static String bytesToHex(byte[] hash) { |
||
36 | StringBuffer hexString = new StringBuffer(); |
||
37 | for (int i = 0; i < hash.length; i++) { |
||
38 | String hex = Integer.toHexString(0xff & hash[i]); |
||
39 | if(hex.length() == 1) hexString.append('0'); |
||
40 | hexString.append(hex); |
||
41 | } |
||
42 | return hexString.toString();
|
||
43 | } |
||
44 | |||
45 | public static String hash(String originalString) throws NoSuchAlgorithmException { |
||
46 | MessageDigest digest = MessageDigest.getInstance("SHA-256"); |
||
47 | |||
48 | byte[] encodedHash = digest.digest(originalString.getBytes(StandardCharsets.UTF_8)); |
||
49 | String hashedID = bytesToHex(encodedHash);
|
||
50 | System.out.println("" + encodedHash.length + " : " + hashedID); |
||
51 | return hashedID;
|
||
52 | } |
||
53 | |||
54 | } |