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.format;
020
021import java.util.Locale;
022
023/**
024 * An enum representing Shiro's default provided {@link HashFormat} implementations.
025 *
026 * @since 1.2
027 */
028public enum ProvidedHashFormat {
029
030    /**
031     * Value representing the {@link HexFormat} implementation.
032     */
033    HEX(HexFormat.class),
034
035    /**
036     * Value representing the {@link Base64Format} implementation.
037     */
038    BASE64(Base64Format.class),
039
040    /**
041     * Value representing the {@link Shiro1CryptFormat} implementation.
042     */
043    SHIRO1(Shiro1CryptFormat.class);
044
045    private final Class<? extends HashFormat> clazz;
046
047    private ProvidedHashFormat(Class<? extends HashFormat> clazz) {
048        this.clazz = clazz;
049    }
050
051    Class<? extends HashFormat> getHashFormatClass() {
052        return this.clazz;
053    }
054
055    public static ProvidedHashFormat byId(String id) {
056        if (id == null) {
057            return null;
058        }
059        try {
060            // Use English Locale, some Locales handle uppercase/lower differently. i.e. Turkish and upper case 'i'
061            // is not 'I'. And 'SHIRO1' would be 'SHÄ°RO1'
062            return valueOf(id.toUpperCase(Locale.ENGLISH));
063        } catch (IllegalArgumentException ignored) {
064            return null;
065        }
066    }
067
068}