/** * Copyright (c) 2011-2019, James Zhan 詹波 (jfinal@126.com). * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.jfinal.kit; import java.security.MessageDigest; public class HashKit { public static final long FNV_OFFSET_BASIS_64 = 0xcbf29ce484222325L; public static final long FNV_PRIME_64 = 0x100000001b3L; private static final java.security.SecureRandom random = new java.security.SecureRandom(); private static final char[] HEX_DIGITS = "0123456789abcdef".toCharArray(); private static final char[] CHAR_ARRAY = "_-0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ".toCharArray(); public static long fnv1a64(String key) { long hash = FNV_OFFSET_BASIS_64; for(int i=0, size=key.length(); i> 4) & 0x0f]); ret.append(HEX_DIGITS[bytes[i] & 0x0f]); } return ret.toString(); } /** * md5 128bit 16bytes * sha1 160bit 20bytes * sha256 256bit 32bytes * sha384 384bit 48bytes * sha512 512bit 64bytes */ public static String generateSalt(int saltLength) { StringBuilder salt = new StringBuilder(saltLength); for (int i=0; i