001/* 002 * Copyright (c) 2015-2020, Oracle and/or its affiliates. All rights reserved. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 017package org.tribuo.hash; 018 019import com.oracle.labs.mlrg.olcut.config.Configurable; 020import com.oracle.labs.mlrg.olcut.provenance.ConfiguredObjectProvenance; 021import com.oracle.labs.mlrg.olcut.provenance.Provenancable; 022 023import java.io.Serializable; 024 025/** 026 * An abstract base class for hash functions used to hash the names of features. 027 */ 028public abstract class Hasher implements Configurable, Provenancable<ConfiguredObjectProvenance>, Serializable { 029 private static final long serialVersionUID = 2L; 030 031 /** 032 * The minimum length of the salt. Salts shorter than this will not validate. 033 */ 034 public static final int MIN_LENGTH=8; 035 036 /** 037 * Hashes the supplied input using the hashing function. 038 * @param input The input to hash. 039 * @return A String representation of the hashed output. 040 */ 041 public abstract String hash(String input); 042 043 /** 044 * The salt is transient, it must be set **to the same value as it was trained with** 045 * after the {@link org.tribuo.Model} is deserialized. 046 * @param salt Salt value. 047 */ 048 public abstract void setSalt(String salt); 049 050 /** 051 * Salt validation is currently a test to see if the string is longer than {@link Hasher#MIN_LENGTH}. 052 * <p> 053 * When this method is updated Hasher must update it's serialVersionUID, this ensures that 054 * serialised instances of downstream classes which call this method are invalidated, as 055 * changes to validateSalt may invalidate old salts, and there is no other way 056 * to communicate this to the developer. 057 * @param salt String to validate. 058 * @return True if the salt is valid, false otherwise. 059 */ 060 public static boolean validateSalt(String salt) { 061 return salt.length() > MIN_LENGTH; 062 } 063 064}