This project has retired. For details please refer to its
Attic page.
AbstractJAASPasswordEncoder xref
1 package org.apache.archiva.redback.policy.encoders;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 import org.apache.archiva.redback.policy.PasswordEncoder;
20 import org.apache.archiva.redback.policy.PasswordEncodingException;
21 import org.apache.archiva.redback.users.Messages;
22 import org.apache.commons.codec.binary.Base64;
23 import org.apache.commons.lang.StringUtils;
24
25 import java.io.UnsupportedEncodingException;
26 import java.security.MessageDigest;
27 import java.security.NoSuchAlgorithmException;
28
29
30
31
32
33
34
35 public class AbstractJAASPasswordEncoder
36 implements PasswordEncoder
37 {
38 private String algorithm;
39
40 private Object systemSalt;
41
42 public AbstractJAASPasswordEncoder( String algorithm )
43 {
44 this.algorithm = algorithm;
45 }
46
47 public void setSystemSalt( Object salt )
48 {
49 this.systemSalt = salt;
50 }
51
52 public String encodePassword( String rawPass, Object salt )
53 {
54 if ( rawPass == null )
55 {
56 throw new IllegalArgumentException( "rawPass parameter cannot be null." );
57 }
58
59 MessageDigest md = null;
60 try
61 {
62 md = MessageDigest.getInstance( this.algorithm );
63 String precode = rawPass;
64
65
66
67 if ( salt != null )
68 {
69
70 precode += "{" + salt + "}";
71 }
72 md.update( precode.getBytes( "UTF-8" ) );
73
74 byte raw[] = md.digest();
75 Base64 base64 = new Base64( 0, new byte[0] );
76 return ( base64.encodeToString( raw ) );
77 }
78 catch ( NoSuchAlgorithmException e )
79 {
80 throw new PasswordEncodingException(
81 Messages.getString( "password.encoder.no.such.algoritm", this.algorithm ), e );
82 }
83 catch ( UnsupportedEncodingException e )
84 {
85 throw new PasswordEncodingException( Messages.getString( "password.encoder.unsupported.encoding" ),
86 e );
87 }
88 }
89
90 public boolean isPasswordValid( String encPass, String rawPass, Object salt )
91 {
92 if ( StringUtils.isEmpty( encPass ) )
93 {
94
95 return false;
96 }
97
98
99
100
101
102
103
104
105
106 String testPass = encodePassword( rawPass, salt );
107 return ( encPass.equals( testPass ) );
108 }
109
110 public String encodePassword( String rawPass )
111 {
112 return encodePassword( rawPass, this.systemSalt );
113 }
114
115 public boolean isPasswordValid( String encPass, String rawPass )
116 {
117 return isPasswordValid( encPass, rawPass, this.systemSalt );
118 }
119
120 }