1 /* 2 * Copyright (C) 2023 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.adservices.service.signals; 18 19 import static com.android.adservices.service.signals.HexEncodingUtil.binaryToHex; 20 import static com.android.adservices.service.signals.HexEncodingUtil.hexToBinary; 21 22 import com.google.auto.value.AutoValue; 23 24 import java.time.Instant; 25 import java.util.Base64; 26 27 /** Internal representation of a custom protected signal's value and metadata. */ 28 @AutoValue 29 public abstract class ProtectedSignal { 30 31 // 60 days 32 public static final int EXPIRATION_SECONDS = 60 * 24 * 60 * 60; 33 34 /** 35 * @return The value of this signal encoded in a Hex string 36 */ getHexEncodedValue()37 public abstract String getHexEncodedValue(); 38 39 /** 40 * @return The value of this signal encoded in a Base64 string 41 */ getBase64EncodedValue()42 public String getBase64EncodedValue() { 43 byte[] binary = hexToBinary(getHexEncodedValue()); 44 return Base64.getEncoder().encodeToString(binary); 45 } 46 47 /** 48 * @return The time the signal was creation 49 */ getCreationTime()50 public abstract Instant getCreationTime(); 51 52 /** 53 * @return The package name that created the signal. 54 */ getPackageName()55 public abstract String getPackageName(); 56 builder()57 static Builder builder() { 58 return new AutoValue_ProtectedSignal.Builder(); 59 } 60 61 @AutoValue.Builder 62 abstract static class Builder { setHexEncodedValue(String value)63 abstract Builder setHexEncodedValue(String value); 64 65 /** 66 * @throws IllegalArgumentException If {@code value} is not Base64 encoded 67 */ setBase64EncodedValue(String value)68 Builder setBase64EncodedValue(String value) { 69 byte[] binary = Base64.getDecoder().decode(value); 70 return setHexEncodedValue(binaryToHex(binary)); 71 } 72 setCreationTime(Instant creationTime)73 abstract Builder setCreationTime(Instant creationTime); 74 setPackageName(String packageName)75 abstract Builder setPackageName(String packageName); 76 build()77 abstract ProtectedSignal build(); 78 } 79 } 80