aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/org/gnunet/util/HashCode.java
blob: 8c678e8c5e3873c8482610f5273e44a4bbc9af66 (plain) (blame)
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
/*
 This file is part of GNUnet.
 Copyright (C) 2011, 2012 Christian Grothoff (and other contributing authors)

 GNUnet is free software; you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published
 by the Free Software Foundation; either version 3, or (at your
 option) any later version.

 GNUnet is distributed in the hope that it will be useful, but
 WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with GNUnet; see the file COPYING.  If not, write to the
 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 Boston, MA 02110-1301, USA.
 */

package org.gnunet.util;


import com.google.common.base.Charsets;
import org.gnunet.construct.FixedSizeIntegerArray;
import org.gnunet.construct.Message;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.util.Arrays;


/**
 * 512-bit hash code
 */
public class HashCode implements Message {

    @FixedSizeIntegerArray(length = 64, signed = false, bitSize = 8)
    public byte[] data; // should be immutable, final, can't be due to construct

    /**
     * Create a hash code initialized to zero.
     */
    public HashCode() {
        data = new byte[64];
    }

    /**
     * Create a HashCode from an existing SHA-512 hash code value.
     *
     * @param hash SHA-512 hash code value
     * @return a HashCode
     */
    public static HashCode fromHashCode(byte[] hash) {
        return new HashCode(hash);
    }

    /**
     * Create a HashCode from data to be hashed.
     *
     * @param data data to hash
     * @return a HashCode
     */
    public static HashCode hash(byte[] data) {
        MessageDigest digest;
        try {
            digest = MessageDigest.getInstance("SHA-512");
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException("crypto algorithm 'SHA-512' required but not provided");
        }
        byte[] hb = digest.digest(data);
        HashCode h = new HashCode(hb);
        return h;
    }

    /**
     * Private constructor for HashCode from the hash code value.  Made
     * private because there are two ways to create hash codes from byte arrays.
     *
     * @param hash hash code value to store in the HashCode
     */
    private HashCode(byte[] hash) {
        if (hash.length != 64) {
            throw new AssertionError("HashCode has to have length 64");
        }
        data = Arrays.copyOf(hash, hash.length);
    }

    /**
     * Create the HashCode of an UTF-8 String using SHA-512.
     *
     * @param s the string to hash
     */
    public HashCode(String s) {
        MessageDigest digest;
        try {
            digest = MessageDigest.getInstance("SHA-512");
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException("crypto algorithm required but not provided");
        }
        byte[] data = digest.digest(s.getBytes(Charsets.UTF_8));
        if (data.length != 64) {
            throw new RuntimeException("error in SHA512 algorithm");
        }
        this.data = data;
    }

    public boolean isAllZero() {
        for (byte aData : data) {
            if (aData != 0) {
                return false;
            }
        }
        return true;
    }

    @Override
    public boolean equals(Object other) {
        if (!(other instanceof HashCode)) {
            return false;
        }
        HashCode hashCode = (HashCode) other;
        return Arrays.equals(this.data, hashCode.data);
    }

    @Override
    public int hashCode() {
        return Arrays.hashCode(this.data);
    }

    @Override
    public String toString() {
        return Strings.dataToString(data);
    }


    public static HashCode random() {
        HashCode hashCode = new HashCode();
        SecureRandom sr = new SecureRandom();
        sr.nextBytes(hashCode.data);
        return hashCode;
    }
}