001// -------------------------------------------------------------------------------- 002// Copyright 2002-2024 Echo Three, LLC 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 or implied. 013// See the License for the specific language governing permissions and 014// limitations under the License. 015// -------------------------------------------------------------------------------- 016 017package com.echothree.util.server.persistence; 018 019import com.echothree.util.common.persistence.BaseKey; 020import com.google.common.base.Charsets; 021import com.google.common.io.BaseEncoding; 022import java.security.MessageDigest; 023import java.security.NoSuchAlgorithmException; 024 025public class Sha1Utils { 026 027 private Sha1Utils() { 028 super(); 029 } 030 031 private static class Sha1UtilsHolder { 032 static Sha1Utils instance = new Sha1Utils(); 033 } 034 035 public static Sha1Utils getInstance() { 036 return Sha1UtilsHolder.instance; 037 } 038 039 public String generateSalt() { 040 byte[] salt = new byte[8]; 041 042 EncryptionUtils.getInstance().getRandom().nextBytes(salt); 043 044 return BaseEncoding.base64().encode(salt); 045 } 046 047 public String encode(String salt, String password) { 048 byte[] input; 049 050 try { 051 MessageDigest sha1Encoder = MessageDigest.getInstance("SHA-1"); 052 053 sha1Encoder.reset(); 054 sha1Encoder.update(BaseEncoding.base64().decode(salt)); 055 input = sha1Encoder.digest(password.getBytes(Charsets.UTF_8)); 056 057 for(int i = 0; i < 1000; i++) { 058 sha1Encoder.reset(); 059 input = sha1Encoder.digest(input); 060 } 061 } catch (NoSuchAlgorithmException nsae) { 062 input = null; 063 } 064 065 return BaseEncoding.base64().encode(input); 066 } 067 068 public String encode(BaseKey baseKey1, BaseKey baseKey2) { 069 byte[] input; 070 071 try { 072 MessageDigest sha1Encoder = MessageDigest.getInstance("SHA-1"); 073 074 sha1Encoder.reset(); 075 sha1Encoder.update(baseKey1.getKey().getEncoded()); 076 sha1Encoder.update(baseKey1.getIv()); 077 sha1Encoder.update(baseKey2.getKey().getEncoded()); 078 sha1Encoder.update(baseKey2.getIv()); 079 input = sha1Encoder.digest(); 080 } catch (NoSuchAlgorithmException nsae) { 081 input = null; 082 } 083 084 return BaseEncoding.base64().encode(input); 085 } 086 087 public String hash(String string) { 088 return hash(string.getBytes(Charsets.UTF_8)); 089 } 090 091 public String hash(byte[] bytes) { 092 byte[] input; 093 094 try { 095 MessageDigest sha1Encoder = MessageDigest.getInstance("SHA-1"); 096 097 sha1Encoder.reset(); 098 sha1Encoder.update(bytes); 099 input = sha1Encoder.digest(); 100 } catch (NoSuchAlgorithmException nsae) { 101 input = null; 102 } 103 104 return BaseEncoding.base64().encode(input); 105 } 106 107}