package com.airlock.iam.common.domain.model.jwt;

import com.airlock.iam.base.misc.util.Base64Util;
import com.google.common.base.Preconditions;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import java.util.Arrays;

public final class JwtSecret {
   private final byte[] secret;

   private JwtSecret(String secret) {
      Preconditions.checkArgument(secret.length() == 128, "secret must be exactly 128 characters long");
      this.secret = Base64Util.decode(secret);
   }

   public static JwtSecret jwtSecret(String secret) {
      return new JwtSecret(secret);
   }

   public JwtKeys getJwtKeys() {
      byte[] hmacKey = Arrays.copyOfRange(this.secret, 0, 64);
      String encodedHmacKey = Base64Util.encodeToString(hmacKey);
      byte[] encryptionKey = Arrays.copyOfRange(this.secret, 64, 96);
      String encodedEncryptionKey = Base64Util.encodeToString(encryptionKey);
      return JwtKeys.jwtKeys(encodedHmacKey, encodedEncryptionKey);
   }

   @SuppressFBWarnings(justification = "generated code")
   @Override
   public boolean equals(Object o) {
      if (o == this) {
         return true;
      } else {
         return !(o instanceof JwtSecret other) ? false : Arrays.equals(this.secret, other.secret);
      }
   }

   @SuppressFBWarnings(justification = "generated code")
   @Override
   public int hashCode() {
      int PRIME = 59;
      int result = 1;
      return result * 59 + Arrays.hashCode(this.secret);
   }
}
