-
Notifications
You must be signed in to change notification settings - Fork 135
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
139 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
/* | ||
* Unless explicitly stated otherwise all files in this repository are licensed under the Apache License Version 2.0. | ||
* This product includes software developed at Datadog (https://www.datadoghq.com/). | ||
* Copyright 2019-2020 Datadog, Inc. | ||
*/ | ||
|
||
import Foundation | ||
import CryptoKit | ||
import CommonCrypto | ||
|
||
/// Computes SHA256 for given `string`. | ||
/// | ||
/// The implementation is portable between iOS versions - it uses `CryptoKit` on iOS13+ and fallbacks | ||
/// to `CommonCrypto` on older devices. | ||
internal func sha256(_ string: String) -> String { | ||
guard let data = string.data(using: .utf8) else { | ||
return string | ||
} | ||
|
||
if #available(iOS 13.0, tvOS 13.0, *) { // Compute SHA256 with `CryptoKit` | ||
let digest = SHA256.hash(data: data) | ||
return digest | ||
.map({ byte in String(format: "%02x", byte) }) | ||
.joined(separator: "") | ||
} else { // Fallback to SHA256 with `CommonCrypto` | ||
var digest: [UInt8] = Array(repeating: 0, count: Int(CC_SHA256_DIGEST_LENGTH)) | ||
_ = data.withUnsafeBytes { CC_SHA256($0.baseAddress, UInt32(data.count), &digest) } | ||
return digest | ||
.map({ byte in String(format: "%02x", byte) }) | ||
.joined(separator: "") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
Tests/DatadogTests/Datadog/Core/Utils/CryptographyTests.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
/* | ||
* Unless explicitly stated otherwise all files in this repository are licensed under the Apache License Version 2.0. | ||
* This product includes software developed at Datadog (https://www.datadoghq.com/). | ||
* Copyright 2019-2020 Datadog, Inc. | ||
*/ | ||
|
||
import XCTest | ||
@testable import Datadog | ||
|
||
class CryptographyTests: XCTestCase { | ||
func testItComputesSHA256ForArbitraryString() { | ||
(0..<20).forEach { _ in | ||
// Given | ||
let string: String = .mockRandom(among: .allUnicodes, length: .mockRandom(min: 1, max: 500)) | ||
|
||
// When | ||
let sha = sha256(string) | ||
|
||
// Then | ||
XCTAssertEqual(sha.count, 64, "It must use 64 characters") | ||
XCTAssertFalse(sha.contains(where: { !$0.isASCII }), "It must contain only ASCII characters") | ||
} | ||
} | ||
|
||
func testWhenComputingSHA256_itGivesStableResults() { | ||
XCTAssertEqual(sha256(""), "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855") | ||
XCTAssertEqual(sha256("foo"), "2c26b46b68ffc68ff99b453c1d30413413422d706483bfa0f98a5e886266e7ae") | ||
XCTAssertEqual(sha256("Bar Bizz"), "ad81f738a40902ef4bb3e4d5f5b83d3e2b3b7edfcd1669ddd4004d4815d03a75") | ||
XCTAssertEqual(sha256(".//,"), "822236197264817e14fdd2939d9dc68c7d0151a6265798dd29511315cb428c66") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters