private SecureUTF8String runAlgorithm ( SecureUTF8String masterPassword , SecureUTF8String data , Account account ) throws Exception { SecureUTF8String output = null ; SecureCharArray digestChars = null ; SecureByteArray masterPasswordBytes = null ; SecureByteArray dataBytes = null ; try { masterPasswordBytes = new SecureByteArray ( masterPassword ) ; dataBytes = new SecureByteArray ( data ) ; if ( ! account . isHmac ( ) ) { dataBytes . prepend ( masterPasswordBytes ) ; } if ( account . isHmac ( ) ) { Mac mac ; String algoName = "HMAC" + account . getAlgorithm ( ) . getName ( ) ; mac = Mac . getInstance ( algoName , CRYPTO_PROVIDER ) ; mac . init ( new SecretKeySpec ( masterPasswordBytes . getData ( ) , algoName ) ) ; mac . reset ( ) ; mac . update ( dataBytes . getData ( ) ) ; digestChars = new SecureCharArray ( mac . doFinal ( ) ) ; } else { MessageDigest md = MessageDigest . getInstance ( account . getAlgorithm ( ) . getName ( ) , CRYPTO_PROVIDER ) ; digestChars = new SecureCharArray ( md . digest ( dataBytes . getData ( ) ) ) ; } output = rstr2any ( digestChars . getData ( ) , account . getCharacterSet ( ) , account . isTrim ( ) ) ; } catch ( Exception e ) { if ( output != null ) output . erase ( ) ; throw e ; } finally { if ( masterPasswordBytes != null ) masterPasswordBytes . erase ( ) ; if ( dataBytes != null ) dataBytes . erase ( ) ; if ( digestChars != null ) digestChars . erase ( ) ; } return output ; }
This performs the actual hashing . It obtains an instance of the hashing algorithm and feeds in the necessary data .