@@ -158,12 +158,15 @@ String getRealName() {
158158 return osslToJava (name );
159159 }
160160
161- MessageDigest getDigestImpl () {
161+ MessageDigest getDigest () {
162+ if (digest == null ) {
163+ throw getRuntime ().newRuntimeError ("Digest CTX wasn't initialized!" );
164+ }
162165 return digest ;
163166 }
164167
165168 public String getName () {
166- return name .toString ();
169+ return name != null ? name .toString () : null ;
167170 }
168171
169172 @ JRubyMethod (required = 1 , optional = 1 , visibility = Visibility .PRIVATE )
@@ -186,9 +189,10 @@ public IRubyObject initialize_copy(final IRubyObject obj) {
186189 checkFrozen ();
187190 if ( this == obj ) return this ;
188191 final Digest that = ((Digest ) obj );
192+ final MessageDigest thatDigest = that .getDigest ();
189193 this .name = (RubyString ) that .name .dup ();
190194 try {
191- this .digest = (MessageDigest ) that . digest .clone ();
195+ this .digest = (MessageDigest ) thatDigest .clone ();
192196 }
193197 catch (CloneNotSupportedException e ) {
194198 final Ruby runtime = getRuntime ();
@@ -201,36 +205,39 @@ public IRubyObject initialize_copy(final IRubyObject obj) {
201205 @ JRubyMethod (name = "update" , alias = "<<" )
202206 public IRubyObject update (final IRubyObject obj ) {
203207 final ByteList bytes = obj .asString ().getByteList ();
204- digest .update (bytes .getUnsafeBytes (), bytes .getBegin (), bytes .getRealSize ());
208+ getDigest () .update (bytes .getUnsafeBytes (), bytes .getBegin (), bytes .getRealSize ());
205209 return this ;
206210 }
207211
208212 @ JRubyMethod
209213 public IRubyObject reset () {
210- digest .reset ();
214+ getDigest () .reset ();
211215 return this ;
212216 }
213217
214218 @ JRubyMethod
215219 public RubyString finish () {
216- final byte [] hash = digest .digest ();
217- digest .reset ();
220+ final MessageDigest md = getDigest ();
221+ final byte [] hash = md .digest ();
222+ md .reset ();
218223 return newString (getRuntime (), hash );
219224 }
220225
221226 @ JRubyMethod
222- public RubyString name () { return name ; }
227+ public RubyString name () {
228+ return name ;
229+ }
223230
224231 @ JRubyMethod
225232 public IRubyObject digest_length () {
226- return RubyFixnum .newFixnum (getRuntime (), digest .getDigestLength ());
233+ return RubyFixnum .newFixnum (getRuntime (), getDigest () .getDigestLength ());
227234 }
228235
229236 @ JRubyMethod
230237 public RubyInteger block_length (final ThreadContext context ) {
231238 final Ruby runtime = context .runtime ;
232239
233- final int blockLength = getBlockLength ( digest .getAlgorithm () );
240+ final int blockLength = getBlockLength ( getDigest () .getAlgorithm () );
234241
235242 if ( blockLength == -1 ) {
236243 throw runtime .newRuntimeError (getMetaClass () + " doesn't implement block_length()" );
@@ -239,7 +246,7 @@ public RubyInteger block_length(final ThreadContext context) {
239246 }
240247
241248 String getAlgorithm () {
242- return this . digest .getAlgorithm ();
249+ return getDigest () .getAlgorithm ();
243250 }
244251
245252 String getShortAlgorithm () {
0 commit comments