5 // Created by Dongri Jin on 1/28/15.
6 // Copyright (c) 2015 Dongri Jin. All rights reserved.
15 class internal func sha1(key: Data, message: Data) -> Data? {
18 let message = message.bytes
20 if key.count > blockSize {
21 key = SHA1(key).calculate()
22 } else if key.count < blockSize { // padding
23 key += [UInt8](repeating: 0, count: blockSize - key.count)
26 var ipad = [UInt8](repeating: 0x36, count: blockSize)
27 for idx in key.indices {
28 ipad[idx] = key[idx] ^ ipad[idx]
31 var opad = [UInt8](repeating: 0x5c, count: blockSize)
32 for idx in key.indices {
33 opad[idx] = key[idx] ^ opad[idx]
36 let ipadAndMessageHash = SHA1(ipad + message).calculate()
37 let mac = SHA1(opad + ipadAndMessageHash).calculate()
39 return Data(bytes: UnsafePointer<UInt8>(mac), count: mac.count)
45 extension HMAC: OAuthSwiftSignatureDelegate {
46 open static func sign(hashMethod: OAuthSwiftHashMethod, key: Data, message: Data) -> Data? {
49 return sha1(key: key, message: message)
51 assertionFailure("Must no sign with none")