-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #89 from dwyl/sha256-hash
Converting the string to a SHA256 hash
- Loading branch information
Showing
4 changed files
with
48 additions
and
1 deletion.
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
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,14 @@ | ||
// | ||
// dwyl-Bridging-Header.h | ||
// dwyl | ||
// | ||
// Created by Sohil Pandya on 22/06/2017. | ||
// Copyright © 2017 dwyl. All rights reserved. | ||
// | ||
|
||
#ifndef dwyl_Bridging_Header_h | ||
#define dwyl_Bridging_Header_h | ||
|
||
#import "CommonCrypto/CommonCrypto.h" | ||
|
||
#endif /* dwyl_Bridging_Header_h */ |
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,24 @@ | ||
// | ||
// stringToHash.swift | ||
// dwyl | ||
// | ||
// Created by Sohil Pandya on 22/06/2017. | ||
// Copyright © 2017 dwyl. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
|
||
func sha256(_ data: Data) -> Data? { | ||
guard let res = NSMutableData(length: Int(CC_SHA256_DIGEST_LENGTH)) else { return nil } | ||
CC_SHA256((data as NSData).bytes, CC_LONG(data.count), res.mutableBytes.assumingMemoryBound(to: UInt8.self)) | ||
return res as Data | ||
} | ||
|
||
func StringToHash(_ str: String) -> String? { | ||
guard | ||
let data = str.data(using: String.Encoding.utf8), | ||
let shaData = sha256(data) | ||
else { return nil } | ||
let rc = shaData.base64EncodedString(options: []) | ||
return rc | ||
} |