@@ -17,10 +17,11 @@ #import "OFHMAC.h" #import "OFSecureData.h" #import "OFHashAlreadyCalculatedException.h" +#import "OFHashNotCalculatedException.h" #import "OFInvalidArgumentException.h" @implementation OFHMAC @synthesize hashClass = _hashClass; @synthesize allowsSwappableMemory = _allowsSwappableMemory; @@ -82,10 +83,11 @@ if (length > blockSize) { id hash = [_hashClass hashWithAllowsSwappableMemory: _allowsSwappableMemory]; [hash updateWithBuffer: key length: length]; + [hash calculate]; length = hash.digestSize; if OF_UNLIKELY (length > blockSize) length = blockSize; @@ -138,21 +140,30 @@ exceptionWithObject: self]; [_innerHash updateWithBuffer: buffer length: length]; } -- (const unsigned char *)digest +- (void)calculate { + if (_calculated) + @throw [OFHashAlreadyCalculatedException + exceptionWithObject: self]; + if (_outerHash == nil || _innerHash == nil) @throw [OFInvalidArgumentException exception]; - if (_calculated) - return _outerHash.digest; - + [_innerHash calculate]; [_outerHash updateWithBuffer: _innerHash.digest length: _innerHash.digestSize]; + [_outerHash calculate]; _calculated = true; +} + +- (const unsigned char *)digest +{ + if (!_calculated) + @throw [OFHashNotCalculatedException exceptionWithObject: self]; return _outerHash.digest; } - (size_t)digestSize