KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > snow > crypto > SecretKeyUtilities


1 package snow.crypto;
2
3 import snow.utils.storage.*;
4
5 import java.security.*;
6 import java.security.spec.*;
7 import javax.crypto.spec.*;
8 import javax.crypto.*;
9 import java.io.*;
10 import java.util.zip.*;
11 import java.util.*;
12
13
14 public final class SecretKeyUtilities
15 {
16
17   private SecretKeyUtilities()
18   {
19
20   } // Constructor
21

22   /** take the first length_in_bytes bytes (*8 = bits) of the sha-1 hash of pass
23      WARNING: this is really secret, rehash it to generate a signature
24      @param length_in_bytes up to 16 (64 bits) can be used without restrictions
25         length_in_bytes>16 requires the unrestricted jce policy (download it from sun)
26          make no sense up to 160 bits (20 bytes) because of the use of sha-1
27
28   */

29   public static SecretKey generateSecretKeyFromPassphrase(byte[] pass, int length_in_bytes) throws Exception JavaDoc
30   {
31      byte[] hash = CryptoUtilities.SHA1Hash(pass); // 20 bytes
32
//System.out.println("hapa="+Arrays.toString(hash));
33

34      byte[] wk = new byte[length_in_bytes];
35      System.arraycopy(hash, 0, wk, 0, wk.length); // keep the len first
36
//System.out.println("hapata="+Arrays.toString(wk));
37

38      SecretKeySpec skeySpec = new SecretKeySpec(wk, "Blowfish");
39      //System.out.println("sk="+Arrays.toString(skeySpec.getEncoded()));
40

41      return skeySpec;
42   }
43
44   /** take the 4 first bytes (24 bits) of the hash of the pass
45   */

46   public static SecretKeyID computeSignature(SecretKey key)
47   {
48     try
49     {
50       byte[] hashpass = CryptoUtilities.SHA1Hash(key.getEncoded());
51       byte[] sign = new byte[4];
52       System.arraycopy(hashpass, 0, sign, 0, sign.length);
53       SecretKeyID ski = new SecretKeyID(sign, key.getEncoded().length);
54       return ski;
55     }
56     catch(Exception JavaDoc e)
57     {
58       throw new RuntimeException JavaDoc("Cannot compute key signature");
59     }
60   }
61
62
63
64 } // SecretKeyUtilities
Popular Tags