Home | History | Annotate | Download | only in watchlist
      1 /*
      2  * Copyright (C) 2017 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.server.net.watchlist;
     18 
     19 import com.android.internal.util.HexDump;
     20 
     21 import java.io.FileDescriptor;
     22 import java.io.PrintWriter;
     23 import java.util.Collections;
     24 import java.util.HashSet;
     25 import java.util.List;
     26 import java.util.Set;
     27 
     28 /**
     29  * Helper class to store all harmful digests in memory.
     30  * TODO: Optimize memory usage using byte array with binary search.
     31  */
     32 class HarmfulDigests {
     33 
     34     private final Set<String> mDigestSet;
     35 
     36     HarmfulDigests(List<byte[]> digests) {
     37         final HashSet<String> tmpDigestSet = new HashSet<>();
     38         final int size = digests.size();
     39         for (int i = 0; i < size; i++) {
     40             tmpDigestSet.add(HexDump.toHexString(digests.get(i)));
     41         }
     42         mDigestSet = Collections.unmodifiableSet(tmpDigestSet);
     43     }
     44 
     45     public boolean contains(byte[] digest) {
     46         return mDigestSet.contains(HexDump.toHexString(digest));
     47     }
     48 
     49     public void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
     50         for (String digest : mDigestSet) {
     51             pw.println(digest);
     52         }
     53         pw.println("");
     54     }
     55 }
     56