001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 * http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing,
013 * software distributed under the License is distributed on an
014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 * KIND, either express or implied.  See the License for the
016 * specific language governing permissions and limitations
017 * under the License.
018 */
019package org.apache.shiro.crypto.hash;
020
021import org.apache.shiro.util.ByteSource;
022
023/**
024 * Simple implementation of {@link HashRequest} that can be used when interacting with a {@link HashService}.
025 *
026 * @since 1.2
027 */
028public class SimpleHashRequest implements HashRequest {
029
030    private final ByteSource source; //cannot be null - this is the source to hash.
031    private final ByteSource salt; //null = no salt specified
032    private final int iterations; //0 = not specified by the requestor; let the HashService decide.
033    private final String algorithmName; //null = let the HashService decide.
034
035    /**
036     * Creates a new SimpleHashRequest instance.
037     *
038     * @param algorithmName the name of the hash algorithm to use.  This is often null as the
039     * {@link HashService} implementation is usually configured with an appropriate algorithm name, but this
040     * can be non-null if the hash service's algorithm should be overridden with a specific one for the duration
041     * of the request.
042     *
043     * @param source the source to be hashed
044     * @param salt any public salt which should be used when computing the hash
045     * @param iterations the number of hash iterations to execute.  Zero (0) indicates no iterations were specified
046     * for the request, at which point the number of iterations is decided by the {@code HashService}
047     * @throws NullPointerException if {@code source} is null or empty.
048     */
049    public SimpleHashRequest(String algorithmName, ByteSource source, ByteSource salt, int iterations) {
050        if (source == null) {
051            throw new NullPointerException("source argument cannot be null");
052        }
053        this.source = source;
054        this.salt = salt;
055        this.algorithmName = algorithmName;
056        this.iterations = Math.max(0, iterations);
057    }
058
059    public ByteSource getSource() {
060        return this.source;
061    }
062
063    public ByteSource getSalt() {
064        return this.salt;
065    }
066
067    public int getIterations() {
068        return iterations;
069    }
070
071    public String getAlgorithmName() {
072        return algorithmName;
073    }
074}