1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.jetspeed.security.impl;
18
19 import java.io.ByteArrayOutputStream;
20 import java.io.OutputStream;
21 import java.security.MessageDigest;
22
23 import javax.mail.internet.MimeUtility;
24
25 import org.apache.jetspeed.security.SecurityException;
26 import org.apache.jetspeed.security.spi.CredentialPasswordEncoder;
27
28 public class Jetspeed1CredentialPasswordEncoder implements
29 CredentialPasswordEncoder {
30
31 protected String passwordsAlgorithm = "SHA";
32 protected String encodingMethod = "base64";
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47 public Jetspeed1CredentialPasswordEncoder()
48 {
49 this("SHA", "base64");
50 }
51
52 public Jetspeed1CredentialPasswordEncoder( String algorithm )
53 {
54 this(algorithm, "base64");
55 }
56
57 public Jetspeed1CredentialPasswordEncoder( String algorithm, String encoding )
58 {
59 this.passwordsAlgorithm = algorithm;
60 this.encodingMethod = encoding;
61 }
62
63 public String encode(String userName, String clearTextPassword)
64 throws SecurityException {
65 try
66 {
67 MessageDigest md = MessageDigest.getInstance(passwordsAlgorithm);
68
69
70 byte[] digest = md.digest(clearTextPassword.getBytes("UTF-8"));
71 ByteArrayOutputStream bas = new ByteArrayOutputStream(digest.length + digest.length / 3 + 1);
72 OutputStream encodedStream = MimeUtility.encode(bas, "base64");
73 encodedStream.write(digest);
74 encodedStream.flush();
75 encodedStream.close();
76 return bas.toString();
77 }
78 catch( Exception e )
79 {
80
81 return null;
82 }
83 }
84
85 }