1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package org.apache.directory.server.core.authn;
22
23
24 import org.apache.directory.api.ldap.model.constants.SchemaConstants;
25 import org.apache.directory.api.util.Base64;
26 import org.apache.directory.api.util.Strings;
27
28
29
30
31
32
33
34
35
36
37
38 public class PasswordHistory implements Comparable<PasswordHistory>
39 {
40
41 private String time;
42
43
44 private String syntaxOID = SchemaConstants.OCTET_STRING_SYNTAX;
45
46
47 private int length;
48
49
50 private String data;
51
52 private static final char DELIMITER = '#';
53
54
55
56
57
58
59
60 public PasswordHistory( String pwdHistoryVal )
61 {
62 int pos = pwdHistoryVal.indexOf( DELIMITER );
63 time = pwdHistoryVal.substring( 0, pos );
64
65 pos++;
66 int nextPos = pwdHistoryVal.indexOf( DELIMITER, pos );
67 syntaxOID = pwdHistoryVal.substring( pos, nextPos );
68
69 nextPos++;
70 pos = pwdHistoryVal.indexOf( DELIMITER, nextPos );
71 length = Integer.parseInt( pwdHistoryVal.substring( nextPos, pos ) );
72
73 data = pwdHistoryVal.substring( pos + 1 );
74 }
75
76
77
78
79
80
81
82
83 public PasswordHistory( String time, byte[] password )
84 {
85 this.time = time;
86 this.data = String.valueOf( Base64.encode( password ) );
87 this.length = data.length();
88 }
89
90
91 public byte[] getHistoryValue()
92 {
93 StringBuilder sb = new StringBuilder();
94
95 sb.append( time ).append( DELIMITER );
96
97 sb.append( syntaxOID ).append( DELIMITER );
98
99 sb.append( length ).append( DELIMITER );
100
101 sb.append( data );
102
103 return Strings.getBytesUtf8( sb.toString() );
104 }
105
106
107 public String getTime()
108 {
109 return time;
110 }
111
112
113 public String getSyntaxOID()
114 {
115 return syntaxOID;
116 }
117
118
119 public int getLength()
120 {
121 return length;
122 }
123
124
125 public byte[] getPassword()
126 {
127 return Base64.decode( data.toCharArray() );
128 }
129
130
131
132
133
134 @Override
135 public int compareTo( PasswordHistory o )
136 {
137 return o.getTime().compareTo( time );
138 }
139
140
141
142
143
144 @Override
145 public boolean equals( Object o )
146 {
147 if ( !( o instanceof PasswordHistory ) )
148 {
149 return false;
150 }
151
152 PasswordHistory../../../org/apache/directory/server/core/authn/PasswordHistory.html#PasswordHistory">PasswordHistory other = ( PasswordHistory ) o;
153
154 return this.getTime().equals( other.getTime() ) && this.data.equals( other.data );
155 }
156
157
158 @Override
159 public int hashCode()
160 {
161 final int prime = 31;
162 int result = 1;
163 result = prime * result + ( ( data == null ) ? 0 : data.hashCode() );
164 result = prime * result + length;
165 result = prime * result + ( ( syntaxOID == null ) ? 0 : syntaxOID.hashCode() );
166 result = prime * result + ( ( time == null ) ? 0 : time.hashCode() );
167 return result;
168 }
169
170
171 @Override
172 public String toString()
173 {
174 return "PasswordHistory [time=" + time + ", syntaxOID=" + syntaxOID + ", length=" + length + ", data=" + data
175 + "]";
176 }
177 }