mirror of
https://github.com/haveno-dex/haveno.git
synced 2025-06-20 12:54:37 -04:00
Add DHT mailbox
This commit is contained in:
parent
506ac37293
commit
239389b529
41 changed files with 680 additions and 391 deletions
|
@ -0,0 +1,88 @@
|
|||
/*
|
||||
* 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 io.bitsquare.p2p.MailboxMessage;
|
||||
|
||||
import java.security.KeyPair;
|
||||
|
||||
import java.util.Random;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import static org.springframework.test.util.AssertionErrors.assertEquals;
|
||||
|
||||
public class EncryptionServiceTests {
|
||||
private static final Logger log = LoggerFactory.getLogger(EncryptionServiceTests.class);
|
||||
|
||||
@Test
|
||||
public void testEncryptionWithMailboxMessage() throws Exception {
|
||||
EncryptionService<MailboxMessage> encryptionService = new EncryptionService<>();
|
||||
KeyPair p2pEncryptKeyPair = encryptionService.getKeyPair();
|
||||
|
||||
TestMessage message = new TestMessage("test");
|
||||
EncryptionPackage encryptionPackage = encryptionService.encryptObject(p2pEncryptKeyPair.getPublic(), message);
|
||||
MailboxMessage result = encryptionService.decryptToObject(p2pEncryptKeyPair.getPrivate(), encryptionPackage);
|
||||
assertEquals("", message.data, ((TestMessage) result).data);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testEncryptionWithInteger() throws Exception {
|
||||
EncryptionService<Integer> encryptionService = new EncryptionService<>();
|
||||
KeyPair p2pEncryptKeyPair = encryptionService.getKeyPair();
|
||||
int data = 1234;
|
||||
EncryptionPackage encryptionPackage = encryptionService.encryptObject(p2pEncryptKeyPair.getPublic(), data);
|
||||
Integer result = encryptionService.decryptToObject(p2pEncryptKeyPair.getPrivate(), encryptionPackage);
|
||||
assertEquals("", data, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testEncryptionWithBytes() throws Exception {
|
||||
EncryptionService encryptionService = new EncryptionService();
|
||||
KeyPair p2pEncryptKeyPair = encryptionService.getKeyPair();
|
||||
|
||||
byte[] data = new byte[]{0x00, 0x01, 0x02, 0x03, 0x04};
|
||||
EncryptionPackage encryptionPackage = encryptionService.encrypt(p2pEncryptKeyPair.getPublic(), data);
|
||||
byte[] result = encryptionService.decrypt(p2pEncryptKeyPair.getPrivate(), encryptionPackage);
|
||||
assertEquals("", result, data);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testEncryptionWithLargeData() throws Exception {
|
||||
EncryptionService encryptionService = new EncryptionService();
|
||||
KeyPair p2pEncryptKeyPair = encryptionService.getKeyPair();
|
||||
|
||||
byte[] data = new byte[2000];
|
||||
new Random().nextBytes(data);
|
||||
|
||||
EncryptionPackage encryptionPackage = encryptionService.encrypt(p2pEncryptKeyPair.getPublic(), data);
|
||||
byte[] result = encryptionService.decrypt(p2pEncryptKeyPair.getPrivate(), encryptionPackage);
|
||||
assertEquals("", result, data);
|
||||
}
|
||||
}
|
||||
|
||||
class TestMessage implements MailboxMessage {
|
||||
public String data = "test";
|
||||
|
||||
public TestMessage(String data) {
|
||||
this.data = data;
|
||||
}
|
||||
}
|
|
@ -296,7 +296,7 @@ public class PlaceOfferProtocolTest {
|
|||
|
||||
private Offer getOffer() {
|
||||
return new Offer(OFFER_ID,
|
||||
DSAKeyUtil.generateKeyPair().getPublic(),
|
||||
DSAKeyUtil.generateDSAKeyPair().getPublic(),
|
||||
Direction.BUY,
|
||||
100L,
|
||||
Coin.CENT,
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue