@@ -32,25 +32,15 @@ public AsymmetricSigner() {
3232
3333 @ Override
3434 public String doSign (PrivateKeyManager key , String hashAlgorithm , String plainText ) {
35- /******** INPUT VERIFICATION - BEGIN ********/
36- if (key == null )
37- {
38- error .setError ("AE001" , "Private key cannot be null" );
39- return "" ;
40- }
41- if (hashAlgorithm == null || hashAlgorithm .length () == 0 || SecurityUtils .compareStrings ("" , hashAlgorithm ))
42- {
43- error .setError ("AE002" , "HashAlgorithm cannot be empty value; use HashAlgorithm domain" );
44- return "" ;
45- }
46- if (plainText == null || plainText .length () == 0 || SecurityUtils .compareStrings ("" , plainText ))
47- {
48- error .setError ("AE003" , "The plainText value to sign cannot be empty" );
49- return "" ;
50- }
51- /******** INPUT VERIFICATION - END ********/
52-
35+ this .error .cleanError ();
5336
37+ /*******INPUT VERIFICATION - BEGIN*******/
38+ SecurityUtils .validateObjectInput ("key" , key , this .error );
39+ SecurityUtils .validateStringInput ("hashAlgorithm" , hashAlgorithm , this .error );
40+ SecurityUtils .validateStringInput ("plainText" , plainText , this .error );
41+ if (this .hasError ()) { return "" ;};
42+ /*******INPUT VERIFICATION - END*******/
43+
5444 EncodingUtil eu = new EncodingUtil ();
5545 byte [] inputText = eu .getBytes (plainText );
5646 if (eu .hasError ()) {
@@ -63,30 +53,21 @@ public String doSign(PrivateKeyManager key, String hashAlgorithm, String plainTe
6353 result = sign (key , hashAlgorithm , inputStream );
6454 }catch (Exception e )
6555 {
66- error .setError ("AE004 " , e .getMessage ());
56+ error .setError ("AS001 " , e .getMessage ());
6757 }
6858 return result ;
6959 }
7060
7161 @ Override
7262 public String doSignFile (PrivateKeyManager key , String hashAlgorithm , String path ) {
73- /******** INPUT VERIFICATION - BEGIN ********/
74- if (key == null )
75- {
76- error .setError ("AE005" , "Private key cannot be null" );
77- return "" ;
78- }
79- if (hashAlgorithm == null || hashAlgorithm .length () == 0 || SecurityUtils .compareStrings ("" , hashAlgorithm ))
80- {
81- error .setError ("AE006" , "HashAlgorithm cannot be empty value; use HashAlgorithm domain" );
82- return "" ;
83- }
84- if (path == null || path .length () == 0 || SecurityUtils .compareStrings ("" , path ))
85- {
86- error .setError ("AE007" , "The path value of the file to sign cannot be empty" );
87- return "" ;
88- }
89- /******** INPUT VERIFICATION - END ********/
63+ this .error .cleanError ();
64+
65+ /*******INPUT VERIFICATION - BEGIN*******/
66+ SecurityUtils .validateObjectInput ("key" , key , this .error );
67+ SecurityUtils .validateStringInput ("hashAlgorithm" , hashAlgorithm , this .error );
68+ SecurityUtils .validateStringInput ("path" , path , this .error );
69+ if (this .hasError ()) { return "" ;}
70+ /*******INPUT VERIFICATION - END*******/
9071
9172 String result = "" ;
9273 try (InputStream input = SecurityUtils .getFileStream (path , this .error ))
@@ -98,32 +79,22 @@ public String doSignFile(PrivateKeyManager key, String hashAlgorithm, String pat
9879 result = sign (key , hashAlgorithm , input );
9980 }catch (Exception e )
10081 {
101- error .setError ("AE008 " , e .getMessage ());
82+ error .setError ("AS002 " , e .getMessage ());
10283 }
10384 return result ;
10485 }
10586
10687 @ Override
10788 public boolean doVerify (CertificateX509 cert , String plainText , String signature ) {
108- /******** INPUT VERIFICATION - BEGIN ********/
109- if (cert == null )
110- {
111- error .setError ("AE009" , "Certificate cannot be null" );
112- return false ;
113- }
114- if (plainText == null || plainText .length () == 0 || SecurityUtils .compareStrings ("" , plainText ))
115- {
116- error .setError ("AE010" , "The plainText value to verify cannot be empty" );
117- return false ;
118- }
119- if (signature == null || signature .length () == 0 || SecurityUtils .compareStrings ("" , signature ))
120- {
121- error .setError ("AE011" , "The signature value to verify cannot be empty" );
122- return false ;
123- }
124- /******** INPUT VERIFICATION - END ********/
125-
89+ this .error .cleanError ();
12690
91+ /*******INPUT VERIFICATION - BEGIN*******/
92+ SecurityUtils .validateObjectInput ("cert" , cert , this .error );
93+ SecurityUtils .validateStringInput ("plainText" , plainText , this .error );
94+ SecurityUtils .validateStringInput ("signature" , signature , this .error );
95+ if (this .hasError ()) { return false ;}
96+ /*******INPUT VERIFICATION - END*******/
97+
12798 EncodingUtil eu = new EncodingUtil ();
12899 byte [] inputText = eu .getBytes (plainText );
129100 if (eu .hasError ()) {
@@ -136,30 +107,21 @@ public boolean doVerify(CertificateX509 cert, String plainText, String signature
136107 result = verify (cert , inputStream , signature );
137108 }catch (Exception e )
138109 {
139- error .setError ("AE012 " , e .getMessage () );
110+ error .setError ("AS003 " , e .getMessage () );
140111 }
141112 return result ;
142113 }
143114
144115 @ Override
145116 public boolean doVerifyFile (CertificateX509 cert , String path , String signature ) {
146- /******** INPUT VERIFICATION - BEGIN ********/
147- if (cert == null )
148- {
149- error .setError ("AE013" , "Certificate cannot be null" );
150- return false ;
151- }
152- if (path == null || path .length () == 0 || SecurityUtils .compareStrings ("" , path ))
153- {
154- error .setError ("AE014" , "The path value of the faile to verify cannot be empty" );
155- return false ;
156- }
157- if (signature == null || signature .length () == 0 || SecurityUtils .compareStrings ("" , signature ))
158- {
159- error .setError ("AE015" , "The signature value to verify cannot be empty" );
160- return false ;
161- }
162- /******** INPUT VERIFICATION - END ********/
117+ this .error .cleanError ();
118+
119+ /*******INPUT VERIFICATION - BEGIN*******/
120+ SecurityUtils .validateObjectInput ("cert" , cert , this .error );
121+ SecurityUtils .validateStringInput ("path" , path , this .error );
122+ SecurityUtils .validateStringInput ("signature" , signature , this .error );
123+ if (this .hasError ()) { return false ;}
124+ /*******INPUT VERIFICATION - END*******/
163125
164126 boolean result = false ;
165127 try (InputStream input = SecurityUtils .getFileStream (path , this .error ))
@@ -170,13 +132,13 @@ public boolean doVerifyFile(CertificateX509 cert, String path, String signature)
170132 result = verify (cert , input , signature );
171133 }catch (Exception e )
172134 {
173- error .setError ("AE016 " , e .getMessage ());
135+ error .setError ("AS004 " , e .getMessage ());
174136 }
175137 return result ;
176138 }
177139
178140 /******** EXTERNAL OBJECT PUBLIC METHODS - END ********/
179-
141+
180142 private String sign (PrivateKey key , String hashAlgorithm , InputStream input ) {
181143 PrivateKeyManager keyMan = (PrivateKeyManager ) key ;
182144 if (keyMan .hasError ()) {
@@ -195,14 +157,14 @@ private String sign(PrivateKey key, String hashAlgorithm, InputStream input) {
195157 try {
196158 outputBytes = signer .generateSignature ();
197159 } catch (Exception e ) {
198- error .setError ("AE01 " , e .getMessage ());
160+ error .setError ("AS005 " , e .getMessage ());
199161 return "" ;
200162 }
201163 String result = "" ;
202164 try {
203165 result = Base64 .toBase64String (outputBytes );
204166 } catch (Exception e ) {
205- error .setError ("AE018 " , e .getMessage ());
167+ error .setError ("AS006 " , e .getMessage ());
206168 return "" ;
207169 }
208170 return result ;
@@ -232,19 +194,14 @@ private boolean verify(Certificate certificate, InputStream input, String signat
232194 try {
233195 signatureBytes = Base64 .decode (signature );
234196 } catch (Exception e ) {
235- error .setError ("AE019" , e .getMessage ());
236- return false ;
237- }
238-
239- if (signatureBytes == null || signatureBytes .length == 0 ) {
240- this .error .setError ("AE020" , "Error reading signature" );
197+ error .setError ("AS007" , e .getMessage ());
241198 return false ;
242199 }
243200 boolean result = false ;
244201 try {
245202 result = signer .verifySignature (signatureBytes );
246203 } catch (Exception e ) {
247- error .setError ("AE021 " , e .getMessage ());
204+ error .setError ("AS008 " , e .getMessage ());
248205 return false ;
249206 }
250207 return result ;
@@ -256,7 +213,7 @@ private void setUpSigner(Signer signer, InputStream input, AsymmetricKeyParamete
256213 try {
257214 signer .init (toSign , asymmetricKeyParameter );
258215 } catch (Exception e ) {
259- error .setError ("AE022 " , e .getMessage ());
216+ error .setError ("AS009 " , e .getMessage ());
260217 return ;
261218 }
262219 byte [] buffer = new byte [8192 ];
@@ -266,7 +223,7 @@ private void setUpSigner(Signer signer, InputStream input, AsymmetricKeyParamete
266223 signer .update (buffer , 0 , n );
267224 }
268225 } catch (Exception e ) {
269- error .setError ("AE023 " , e .getMessage ());
226+ error .setError ("AS010 " , e .getMessage ());
270227 return ;
271228 }
272229 }
0 commit comments