1 /* 2 * Copyright (C) 2021 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.systemui.media; 18 19 import java.util.Objects; 20 21 /** 22 * A simple hash function for use in privacy-sensitive logging. 23 */ 24 public final class SmallHash { 25 // Hashes will be in the range [0, MAX_HASH). 26 public static final int MAX_HASH = (1 << 13); 27 28 /** Return Small hash of the string, if non-null, or 0 otherwise. */ hash(String in)29 public static int hash(String in) { 30 return hash(Objects.hashCode(in)); 31 } 32 33 /** 34 * Maps in to the range [0, MAX_HASH), keeping similar values distinct. 35 * 36 * @param in An arbitrary integer. 37 * @return in mod MAX_HASH, signs chosen to stay in the range [0, MAX_HASH). 38 */ hash(int in)39 public static int hash(int in) { 40 return Math.abs(Math.floorMod(in, MAX_HASH)); 41 } 42 SmallHash()43 private SmallHash() {} 44 } 45