• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2021 Google LLC
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //     http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 //
15 ///////////////////////////////////////////////////////////////////////////////
16 
17 package com.google.crypto.tink.testing;
18 
19 import com.google.crypto.tink.hybrid.internal.HpkeUtil;
20 import com.google.crypto.tink.subtle.Hex;
21 import java.util.Arrays;
22 import java.util.Objects;
23 
24 /** Helper class for identifying different test cases. */
25 public final class HpkeTestId {
26   public final byte[] mode;
27   public final byte[] kemId;
28   public final byte[] kdfId;
29   public final byte[] aeadId;
30 
HpkeTestId(byte[] mode, byte[] kemId, byte[] kdfId, byte[] aeadId)31   public HpkeTestId(byte[] mode, byte[] kemId, byte[] kdfId, byte[] aeadId) {
32     this.mode = mode;
33     this.kemId = kemId;
34     this.kdfId = kdfId;
35     this.aeadId = aeadId;
36   }
37 
HpkeTestId(int mode, int kemId, int kdfId, int aeadId)38   public HpkeTestId(int mode, int kemId, int kdfId, int aeadId) {
39     this.mode = HpkeUtil.intToByteArray(1, mode);
40     this.kemId = HpkeUtil.intToByteArray(2, kemId);
41     this.kdfId = HpkeUtil.intToByteArray(2, kdfId);
42     this.aeadId = HpkeUtil.intToByteArray(2, aeadId);
43   }
44 
45   @Override
equals(Object obj)46   public boolean equals(Object obj) {
47     if (!(obj instanceof HpkeTestId)) {
48       return false;
49     }
50     HpkeTestId testId = (HpkeTestId) obj;
51     return Arrays.equals(this.mode, testId.mode)
52         && Arrays.equals(this.kemId, testId.kemId)
53         && Arrays.equals(this.kdfId, testId.kdfId)
54         && Arrays.equals(this.aeadId, testId.aeadId);
55   }
56 
57   @Override
hashCode()58   public int hashCode() {
59     return Objects.hash(
60         Arrays.hashCode(mode),
61         Arrays.hashCode(kemId),
62         Arrays.hashCode(kdfId),
63         Arrays.hashCode(aeadId));
64   }
65 
66   @Override
toString()67   public String toString() {
68     return String.format(
69         "mode=0x%s, kem_id=0x%s, kdf_id=0x%s, aead_id=0x%s",
70         Hex.encode(mode), Hex.encode(kemId), Hex.encode(kdfId), Hex.encode(aeadId));
71   }
72 }
73