Add hash methods

This commit is contained in:
Manfred Karrer 2015-02-16 11:56:34 +01:00
parent 05d2242d24
commit 27e763238e
3 changed files with 38 additions and 0 deletions

View file

@ -30,5 +30,6 @@ public class CryptoModule extends BitsquareModule {
@Override
protected void configure() {
bind(SignatureService.class).asEagerSingleton();
bind(HashService.class).asEagerSingleton();
}
}

View file

@ -0,0 +1,29 @@
/*
* This file is part of Bitsquare.
*
* Bitsquare is free software: you can redistribute it and/or modify it
* under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or (at
* your option) any later version.
*
* Bitsquare 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 Affero General Public
* License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with Bitsquare. If not, see <http://www.gnu.org/licenses/>.
*/
package io.bitsquare.crypto;
import org.bitcoinj.core.Sha256Hash;
import org.bitcoinj.core.Utils;
public class HashService {
public Sha256Hash hash(String message) {
byte[] data = Utils.formatMessageForSigning(message);
return Sha256Hash.createDouble(data);
}
}

View file

@ -30,6 +30,10 @@ public class SignatureService {
public String signMessage(ECKey key, String message) {
byte[] data = Utils.formatMessageForSigning(message);
Sha256Hash hash = Sha256Hash.createDouble(data);
return signMessage(key, hash);
}
public String signMessage(ECKey key, Sha256Hash hash) {
ECKey.ECDSASignature sig = key.sign(hash, null);
// Now we have to work backwards to figure out the recId needed to recover the signature.
int recId = -1;
@ -54,4 +58,8 @@ public class SignatureService {
String signedMessage = signMessage(key, message);
return Utils.sha256hash160(message.concat(signedMessage).getBytes(Charsets.UTF_8));
}
public boolean verify(ECKey key, Sha256Hash hash, ECKey.ECDSASignature signature) {
return key.verify(hash, signature);
}
}