1
- /**
1
+ /*
2
2
* Copyright (c) 2008 Sonatype, Inc. All rights reserved.
3
3
*
4
4
* This program is licensed to you under the Apache License Version 2.0,
15
15
import java .security .Provider ;
16
16
import java .security .Security ;
17
17
import java .util .HashSet ;
18
- import java .util .Iterator ;
19
18
import java .util .Set ;
20
19
import java .util .regex .Matcher ;
21
20
import java .util .regex .Pattern ;
22
21
23
- import javax .enterprise .inject .Typed ;
24
22
import javax .inject .Named ;
23
+ import javax .inject .Singleton ;
24
+
25
+ import org .eclipse .sisu .Typed ;
25
26
26
27
/**
28
+ * Default implementation of {@link PlexusCipher}. This class is thread safe.
29
+ *
27
30
* @author Oleg Gusakov
28
31
*/
32
+ @ Singleton
29
33
@ Named ( "default" )
30
34
@ Typed ( PlexusCipher .class )
31
35
public class DefaultPlexusCipher
32
36
implements PlexusCipher
33
37
{
34
-
35
38
private static final Pattern ENCRYPTED_STRING_PATTERN = Pattern .compile ( ".*?[^\\ \\ ]?\\ {(.*?[^\\ \\ ])\\ }.*" );
36
39
37
40
private final PBECipher _cipher ;
38
41
39
42
// ---------------------------------------------------------------
40
43
public DefaultPlexusCipher ()
41
- throws PlexusCipherException
42
44
{
43
45
_cipher = new PBECipher ();
44
46
}
45
47
46
48
// ---------------------------------------------------------------
49
+ @ Override
47
50
public String encrypt ( final String str , final String passPhrase )
48
51
throws PlexusCipherException
49
52
{
@@ -56,13 +59,15 @@ public String encrypt( final String str, final String passPhrase )
56
59
}
57
60
58
61
// ---------------------------------------------------------------
62
+ @ Override
59
63
public String encryptAndDecorate ( final String str , final String passPhrase )
60
64
throws PlexusCipherException
61
65
{
62
66
return decorate ( encrypt ( str , passPhrase ) );
63
67
}
64
68
65
69
// ---------------------------------------------------------------
70
+ @ Override
66
71
public String decrypt ( final String str , final String passPhrase )
67
72
throws PlexusCipherException
68
73
{
@@ -75,6 +80,7 @@ public String decrypt( final String str, final String passPhrase )
75
80
}
76
81
77
82
// ---------------------------------------------------------------
83
+ @ Override
78
84
public String decryptDecorated ( final String str , final String passPhrase )
79
85
throws PlexusCipherException
80
86
{
@@ -92,6 +98,7 @@ public String decryptDecorated( final String str, final String passPhrase )
92
98
}
93
99
94
100
// ----------------------------------------------------------------------------
101
+ @ Override
95
102
public boolean isEncryptedString ( final String str )
96
103
{
97
104
if ( str == null || str .length () < 1 )
@@ -105,7 +112,7 @@ public boolean isEncryptedString( final String str )
105
112
}
106
113
107
114
// ----------------------------------------------------------------------------
108
- // -------------------
115
+ @ Override
109
116
public String unDecorate ( final String str )
110
117
throws PlexusCipherException
111
118
{
@@ -122,75 +129,67 @@ public String unDecorate( final String str )
122
129
}
123
130
124
131
// ----------------------------------------------------------------------------
125
- // -------------------
132
+ @ Override
126
133
public String decorate ( final String str )
127
134
{
128
135
return ENCRYPTED_STRING_DECORATION_START + ( str == null ? "" : str ) + ENCRYPTED_STRING_DECORATION_STOP ;
129
136
}
130
137
131
138
// ---------------------------------------------------------------
132
- // ---------------------------------------------------------------
133
- // ***************************************************************
139
+
134
140
/**
135
141
* Exploratory part. This method returns all available services types
136
142
*/
137
143
public static String [] getServiceTypes ()
138
144
{
139
- Set result = new HashSet ();
145
+ Set < String > result = new HashSet <> ();
140
146
141
147
// All all providers
142
148
Provider [] providers = Security .getProviders ();
143
- for ( int i = 0 ; i < providers .length ; i ++ )
144
- {
149
+ for (Provider provider : providers ) {
145
150
// Get services provided by each provider
146
- Set keys = providers [i ].keySet ();
147
- for ( Iterator it = keys .iterator (); it .hasNext (); )
148
- {
149
- String key = (String ) it .next ();
150
- key = key .split ( " " )[0 ];
151
-
152
- if ( key .startsWith ( "Alg.Alias." ) )
153
- {
151
+ Set <Object > keys = provider .keySet ();
152
+ for (Object o : keys ) {
153
+ String key = (String ) o ;
154
+ key = key .split (" " )[0 ];
155
+
156
+ if (key .startsWith ("Alg.Alias." )) {
154
157
// Strip the alias
155
- key = key .substring ( 10 );
158
+ key = key .substring (10 );
156
159
}
157
- int ix = key .indexOf ( '.' );
158
- result .add ( key .substring ( 0 , ix ) );
160
+ int ix = key .indexOf ('.' );
161
+ result .add (key .substring (0 , ix ) );
159
162
}
160
163
}
161
- return ( String []) result .toArray ( new String [result .size ()] );
164
+ return result .toArray ( new String [result .size ()] );
162
165
}
163
166
164
167
/**
165
168
* This method returns the available implementations for a service type
166
169
*/
167
170
public static String [] getCryptoImpls ( final String serviceType )
168
171
{
169
- Set result = new HashSet ();
172
+ Set < String > result = new HashSet <> ();
170
173
171
174
// All all providers
172
175
Provider [] providers = Security .getProviders ();
173
- for ( int i = 0 ; i < providers .length ; i ++ )
174
- {
176
+ for (Provider provider : providers ) {
175
177
// Get services provided by each provider
176
- Set keys = providers [i ].keySet ();
177
- for ( Iterator it = keys .iterator (); it .hasNext (); )
178
- {
179
- String key = (String ) it .next ();
180
- key = key .split ( " " )[0 ];
181
-
182
- if ( key .startsWith ( serviceType + "." ) )
183
- {
184
- result .add ( key .substring ( serviceType .length () + 1 ) );
178
+ Set <Object > keys = provider .keySet ();
179
+ for (Object o : keys ) {
180
+ String key = (String ) o ;
181
+ key = key .split (" " )[0 ];
182
+
183
+ if (key .startsWith (serviceType + "." )) {
184
+ result .add (key .substring (serviceType .length () + 1 ));
185
185
}
186
- else if ( key .startsWith ( "Alg.Alias." + serviceType + "." ) )
187
- {
186
+ else if (key .startsWith ("Alg.Alias." + serviceType + "." )) {
188
187
// This is an alias
189
- result .add ( key .substring ( serviceType .length () + 11 ) );
188
+ result .add (key .substring (serviceType .length () + 11 ) );
190
189
}
191
190
}
192
191
}
193
- return ( String []) result .toArray ( new String [result .size ()] );
192
+ return result .toArray ( new String [result .size ()] );
194
193
}
195
194
196
195
// ---------------------------------------------------------------
@@ -201,26 +200,18 @@ public static void main( final String[] args )
201
200
String [] serviceTypes = getServiceTypes ();
202
201
if ( serviceTypes != null )
203
202
{
204
- for ( int i = 0 ; i < serviceTypes .length ; i ++ )
205
- {
206
- String serviceType = serviceTypes [i ];
207
- String [] serviceProviders = getCryptoImpls ( serviceType );
208
- if ( serviceProviders != null )
209
- {
210
- System .out .println ( serviceType + ": provider list" );
211
- for ( int j = 0 ; j < serviceProviders .length ; j ++ )
212
- {
213
- String provider = serviceProviders [j ];
214
- System .out .println ( " " + provider );
203
+ for (String serviceType : serviceTypes ) {
204
+ String [] serviceProviders = getCryptoImpls (serviceType );
205
+ if (serviceProviders != null ) {
206
+ System .out .println (serviceType + ": provider list" );
207
+ for (String provider : serviceProviders ) {
208
+ System .out .println (" " + provider );
215
209
}
216
210
}
217
- else
218
- {
219
- System .out .println ( serviceType + ": does not have any providers in this environment" );
211
+ else {
212
+ System .out .println (serviceType + ": does not have any providers in this environment" );
220
213
}
221
214
}
222
215
}
223
216
}
224
- // ---------------------------------------------------------------
225
- // ---------------------------------------------------------------
226
217
}
0 commit comments