From 7f8ab5bb281099274ff3ed50ecdfd7e5c9dddb0d Mon Sep 17 00:00:00 2001 From: Pavel Tikhonenko Date: Fri, 22 Sep 2017 11:27:45 +0300 Subject: [PATCH] move compute hash data method into validation file --- TPInAppReceipt/Source/InAppReceipt.swift | 20 +------------------- TPInAppReceipt/Source/Validation.swift | 20 ++++++++++++++++++++ 2 files changed, 21 insertions(+), 19 deletions(-) diff --git a/TPInAppReceipt/Source/InAppReceipt.swift b/TPInAppReceipt/Source/InAppReceipt.swift index 2042977..8b3e8aa 100644 --- a/TPInAppReceipt/Source/InAppReceipt.swift +++ b/TPInAppReceipt/Source/InAppReceipt.swift @@ -196,23 +196,5 @@ internal extension InAppReceipt return payload.receiptHash } - /// Computed SHA-1 hash, used to validate the receipt. - /// Should be equal to `receiptHash` value - internal var computedHashData: Data - { - let uuidData = DeviceGUIDRetriever.guid() - let opaqueData = opaqueValue - let bundleIdData = bundleIdentifierData - - var hash = Array(repeating: 0, count: 20) - var ctx = SHA_CTX() - - SHA1_Init(&ctx) - SHA1_Update(&ctx, uuidData.pointer, uuidData.count) - SHA1_Update(&ctx, opaqueData.pointer, opaqueData.count) - SHA1_Update(&ctx, bundleIdData.pointer, bundleIdData.count) - SHA1_Final(&hash, &ctx); - - return Data(bytes: &hash, count: hash.count) - } + } diff --git a/TPInAppReceipt/Source/Validation.swift b/TPInAppReceipt/Source/Validation.swift index d227498..4d67a38 100644 --- a/TPInAppReceipt/Source/Validation.swift +++ b/TPInAppReceipt/Source/Validation.swift @@ -40,6 +40,26 @@ public extension InAppReceipt throw IARError.validationFailed(reason: .hashValidation) } } + + /// Computed SHA-1 hash, used to validate the receipt. + /// Should be equal to `receiptHash` value + internal var computedHashData: Data + { + let uuidData = DeviceGUIDRetriever.guid() + let opaqueData = opaqueValue + let bundleIdData = bundleIdentifierData + + var hash = Array(repeating: 0, count: 20) + var ctx = SHA_CTX() + + SHA1_Init(&ctx) + SHA1_Update(&ctx, uuidData.pointer, uuidData.count) + SHA1_Update(&ctx, opaqueData.pointer, opaqueData.count) + SHA1_Update(&ctx, bundleIdData.pointer, bundleIdData.count) + SHA1_Final(&hash, &ctx); + + return Data(bytes: &hash, count: hash.count) + } } /// A PKCS7Wrapper extension helps to validate the receipt's signature