Skip to content

Commit

Permalink
Add overrides for toString() , equals(..) and hashcode() methods
Browse files Browse the repository at this point in the history
  • Loading branch information
normanmaurer committed Dec 7, 2023
1 parent a161540 commit 7502408
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,31 @@ public AsymmetricCipherKeyPair getKeyPair(OHttpCiphersuite ciphersuite) {
return null;
}

@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}

OHttpServerKeys that = (OHttpServerKeys) o;
return keyMap.equals(that.keyMap);
}

@Override
public String toString() {
return "OHttpServerKeys{" +
"keyMap=" + keyMap +
'}';
}

@Override
public int hashCode() {
return keyMap.hashCode();
}

/*
* Encode {@link ServerKeys} into bytes that represent {@link ServerPublicKeys}, using the format
* described at https://ietf-wg-ohai.github.io/oblivious-http/draft-ietf-ohai-ohttp.html#section-3.1
Expand All @@ -81,6 +106,7 @@ public void encodePublicKeys(HybridPublicKeyEncryption encryption, ByteBuf outpu

output.writeBytes(hpke.serializePublicKey(kp.publicParameters()));

// Multiple by 4 as for each cipher we will write 2 short values.
output.writeShort(key.getValue().ciphers().size() * 4);
for (OHttpKey.Cipher cipher : key.getValue().ciphers()) {
output.writeShort(cipher.kdf().id());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
Expand All @@ -30,12 +31,17 @@
import static io.netty.incubator.codec.hpke.HybridPublicKeyEncryption.AEAD;
import static io.netty.incubator.codec.hpke.HybridPublicKeyEncryption.KDF;
import static io.netty.incubator.codec.hpke.HybridPublicKeyEncryption.KEM;
import static java.util.Objects.requireNonNull;

/**
* Set of server public keys and cipher suites for a OHTTP client.
*/
public final class OHttpServerPublicKeys implements Iterable<Map.Entry<Byte, OHttpKey.PublicKey>> {
private final Map<Byte, OHttpKey.PublicKey> keys = new HashMap<>();
private final Map<Byte, OHttpKey.PublicKey> keys;

public OHttpServerPublicKeys(Map<Byte, OHttpKey.PublicKey> keys) {
this.keys = Collections.unmodifiableMap(requireNonNull(keys, "keys"));
}

/**
* Return all {@link OHttpKey.PublicKey}s.
Expand Down Expand Up @@ -77,7 +83,7 @@ public String toString() {
* Decode a serialized {@link ServerPublicKeys} on the client.
*/
public static OHttpServerPublicKeys decode(ByteBuf input) throws CryptoException {
OHttpServerPublicKeys keys = new OHttpServerPublicKeys();
Map<Byte, OHttpKey.PublicKey> keys = new HashMap<>();
while (input.isReadable()) {
byte keyId = input.readByte();
KEM kem = KEM.forId(input.readShort());
Expand All @@ -92,8 +98,8 @@ public static OHttpServerPublicKeys decode(ByteBuf input) throws CryptoException
ciphers.add(OHttpKey.newCipher(kdf, aead));
}
OHttpKey.PublicKey publicKey = OHttpKey.newPublicKey(keyId, kem, ciphers, publicKeyBytes);
keys.keys.put(keyId, publicKey);
keys.put(keyId, publicKey);
}
return keys;
return new OHttpServerPublicKeys(keys);
}
}

0 comments on commit 7502408

Please sign in to comment.