Created
November 3, 2016 04:33
-
-
Save enix223/d0a812f473cc5cd40c3e2ef542ec0ce7 to your computer and use it in GitHub Desktop.
Calculate SHA1 with Swift2
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
// | |
// TestSHA1Digest.swift | |
// SwiftPlayground | |
// | |
// Created by Enix Yu on 31/10/2016. | |
// Copyright © 2016 RobotBros. All rights reserved. | |
// | |
// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! | |
// !! Be sure to #import <CommonCrypto/CommonCrypto.h> in the bridging header file !! | |
// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! | |
// | |
import UIKit | |
extension String { | |
func sha1() -> String { | |
let data = self.dataUsingEncoding(NSUTF8StringEncoding)! | |
var digest = [UInt8](count:Int(CC_SHA1_DIGEST_LENGTH), repeatedValue: 0) | |
CC_SHA1(data.bytes, CC_LONG(data.length), &digest) | |
let hexBytes = digest.map { String(format: "%02hhx", $0) } | |
return hexBytes.joinWithSeparator("") | |
} | |
func sha1Data() -> NSData { | |
let data = self.dataUsingEncoding(NSUTF8StringEncoding)! | |
var digest = [UInt8](count:Int(CC_SHA1_DIGEST_LENGTH), repeatedValue: 0) | |
CC_SHA1(data.bytes, CC_LONG(data.length), &digest) | |
let digestData = NSData(bytes: digest, length: digest.count) | |
return digestData | |
} | |
} | |
/* | |
Swift 3 | |
extension String { | |
func sha1() -> String { | |
let data = self.data(using: String.Encoding.utf8)! | |
var digest = [UInt8](repeating: 0, count:Int(CC_SHA1_DIGEST_LENGTH)) | |
data.withUnsafeBytes { | |
_ = CC_SHA1($0, CC_LONG(data.count), &digest) | |
} | |
let hexBytes = digest.map { String(format: "%02hhx", $0) } | |
return hexBytes.joined() | |
} | |
} | |
*/ | |
class TestSHA1Digest: UIViewController { | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
if let b64 = calculateDigest("2.045020311114033331503343429780020454510MDAwMDAwMDAwMDAwMDA2ODkzOTI=https://myeshop.gr/orders/Payment_Okhttps://myeshop.gr/orders/Payment_FailEURO123") { | |
print("SHA1: \(b64)") | |
} else { | |
print("Calcuate SHA1 failed") | |
} | |
} | |
func calculateDigest(data: String) -> String? { | |
let shaData = data.sha1Data() | |
return shaData.base64EncodedStringWithOptions(NSDataBase64EncodingOptions(rawValue: 0)) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment