01: package snow.crypto;
02:
03: import snow.utils.storage.*;
04: import java.security.*;
05: import java.security.spec.*;
06: import javax.crypto.spec.*;
07: import javax.crypto.*;
08: import java.io.*;
09: import java.util.zip.*;
10: import java.util.*;
11:
12: public final class SecretKeyUtilities {
13: private SecretKeyUtilities() {
14: }
15:
16: /** take the first length_in_bytes bytes (*8 = bits) of the sha-1 hash of pass
17: WARNING: this is really secret, rehash it to generate a signature
18: @param length_in_bytes up to 16 (64 bits) can be used without restrictions
19: length_in_bytes>16 requires the unrestricted jce policy (download it from sun)
20: make no sense up to 160 bits (20 bytes) because of the use of sha-1
21: */
22: public static SecretKey generateKeyFromPassphrase(byte[] pass,
23: int length_in_bytes) throws Exception {
24: byte[] hash = CryptoUtilities.SHA1Hash(pass);
25: byte[] wk = new byte[length_in_bytes];
26: System.arraycopy(hash, 0, wk, 0, wk.length);
27: SecretKeySpec skeySpec = new SecretKeySpec(wk, "Blowfish");
28: return skeySpec;
29: }
30:
31: /** take the 4 first bytes (24 bits) of the hash of the pass
32: */
33: public static SecretKeyID computeSignature(SecretKey key) {
34: try {
35: byte[] hashpass = CryptoUtilities
36: .SHA1Hash(key.getEncoded());
37: byte[] sign = new byte[4];
38: System.arraycopy(hashpass, 0, sign, 0, sign.length);
39: SecretKeyID ski = new SecretKeyID(sign,
40: key.getEncoded().length);
41: return ski;
42: } catch (Exception e) {
43: throw new RuntimeException("Cannot compute key signature");
44: }
45: }
46:
47: }
|