77import org .cryptomator .cryptolib .common .ECKeyPair ;
88import org .cryptomator .cryptolib .common .P384KeyPair ;
99
10+ import javax .security .auth .Destroyable ;
1011import java .security .interfaces .ECPrivateKey ;
1112import java .security .interfaces .ECPublicKey ;
1213import java .security .spec .InvalidKeySpecException ;
1314import java .text .ParseException ;
1415import java .util .Base64 ;
16+ import java .util .Objects ;
1517
18+ import ch .iterate .hub .client .model .UserDto ;
1619import ch .iterate .hub .crypto .uvf .UvfAccessTokenPayload ;
1720import com .fasterxml .jackson .core .JsonProcessingException ;
1821import com .nimbusds .jose .JOSEException ;
2427 * Represents Cryptomator Hub <a href="https://docs.cryptomator.org/en/latest/security/hub/#user-key-pair>User Keys</a>.
2528 * Counterpart of <a href="https://github.com/cryptomator/hub/blob/develop/frontend/src/common/crypto.ts"><code>UserKeys</code></a>.
2629 */
27- public class UserKeys {
30+ public class UserKeys implements Destroyable {
2831
2932 private final ECKeyPair ecdhKeyPair ;
3033 private final ECKeyPair ecdsaKeyPair ;
@@ -42,6 +45,34 @@ public ECKeyPair ecdsaKeyPair() {
4245 return ecdsaKeyPair ;
4346 }
4447
48+ @ Override
49+ public void destroy () {
50+ ecdhKeyPair .destroy ();
51+ ecdsaKeyPair .destroy ();
52+ }
53+
54+ @ Override
55+ public boolean isDestroyed () {
56+ return ecdhKeyPair .isDestroyed () || ecdsaKeyPair .isDestroyed ();
57+ }
58+
59+ @ Override
60+ public final boolean equals (final Object o ) {
61+ if (!(o instanceof UserKeys )) {
62+ return false ;
63+ }
64+
65+ UserKeys userKeys = (UserKeys ) o ;
66+ return Objects .equals (ecdhKeyPair , userKeys .ecdhKeyPair ) && Objects .equals (ecdsaKeyPair , userKeys .ecdsaKeyPair );
67+ }
68+
69+ @ Override
70+ public int hashCode () {
71+ int result = Objects .hashCode (ecdhKeyPair );
72+ result = 31 * result + Objects .hashCode (ecdsaKeyPair );
73+ return result ;
74+ }
75+
4576 @ Override
4677 public String toString () {
4778 final StringBuilder sb = new StringBuilder ("UserKeys{" );
@@ -55,6 +86,10 @@ public static UserKeys create() {
5586 return new UserKeys (P384KeyPair .generate (), P384KeyPair .generate ());
5687 }
5788
89+ public static boolean validate (final UserDto me ) {
90+ return me .getEcdhPublicKey () != null && me .getPrivateKey () != null ;
91+ }
92+
5893 private UserKeyPayload prepareForEncryption () {
5994 return new UserKeyPayload (Base64 .getEncoder ().encodeToString (ecdhKeyPair ().getPrivate ().getEncoded ()), Base64 .getEncoder ().encodeToString (ecdsaKeyPair ().getPrivate ().getEncoded ()));
6095 }
0 commit comments