Get just the hash from SHA256.hash(data:)? - swift

I'm trying to get just a hash from SHA256.hash(data:) but in order to do this I need to grab the description and then use .replacingOccurrences(of: "SHA256 digest: ", with: ""). Is there a way that I can just get the full SHA256 hash as a string?
func getId<T>(input: T) -> String {
let input = "\(input)".utf8
let data = Data(input)
let hash = SHA256.hash(data: data).description.replacingOccurrences(of: "SHA256 digest: ", with: "")
return hash
}

You can simply map SHA256Digest bytes into an hexa string:
import CryptoKit
extension UnsignedInteger where Self: CVarArg {
var hexa: String { .init(format: "%ll*0x", bitWidth / 4, self) }
}
extension DataProtocol {
var sha256Digest: SHA256Digest { SHA256.hash(data: self) }
var sha256Data: Data { .init(sha256Digest) }
var sha256Hexa: String { sha256Digest.map(\.hexa).joined() }
}
func getId<D: DataProtocol>(data: D) -> String {
data.sha256Hexa
}

Related

How could i convert with an extension of NumberFormatter a String to a Float

I have created an extension of NumberFormatter and binaryInteger, to convert Int to String with a space between thousands like thise: 11111 -> 11 111
Now, in another place, i need to reverse the convertion from a specific string to a Float , like this: 11 111 -> 11111.
Here is the first extensions of NumberFormatter and BinaryInteger:
extension Formatter {
static let withSeparator: NumberFormatter = {
let formatter = NumberFormatter()
formatter.groupingSeparator = " "
formatter.allowsFloats = true
formatter.numberStyle = .decimal
return formatter
}()
}
extension BinaryInteger {
var formattedWithSeparator: String {
return Formatter.withSeparator.string(for: self) ?? ""
}
}
So, how could i code an another extension, to make the reverse process?
thank you.
Try this:
extension String {
func backToFloat() -> Float {
// Make a copy of original string
var temp = self
// Remove spaces
temp.removeAll(where: { $0 == " " })
return Float(temp) ?? 0.0
}
}
print("1 234 567.2".backToFloat())
// log: 1234567.2
To enable Float -> String and Double -> String:
extension FloatingPoint {
var formattedWithSeparator: String {
return Formatter.withSeparator.string(for: self) ?? ""
}
}
print(12345678.12.formattedWithSeparator)
// log: 12 345 678.12
You can use the same withSeparator formatter, and add another extension to BinaryInteger:
extension BinaryInteger {
init?(fromStringWithSeparator string: String) {
if let num = NumberFormatter.withSeparator.number(from: string)
.map({ Self.init(truncatingIfNeeded: $0.int64Value) }) {
self = num
} else {
return nil
}
}
}
Here, I basically parsed the number into an NSNumber, and then converted that to an Int64, then converted that to whatever type of BinaryInteger is required. This won't work for the values of UInt64 that are outside of the range of Int64, as the first conversion will convert them to a negative number. So if you want to handle those numbers as well, you should write another UInt64 extension:
extension UInt64 {
init?(fromStringWithSeparator string: String) {
if let num = NumberFormatter.withSeparator.number(from: string)?.uint64Value {
self = num
} else {
return nil
}
}
}

Swift 5: Why Range<String.Index>.Bound.utf16Offset calculates offsets even if empty string provided as argument

In Swift 4 I had a function to dump Range to debugger output defined as below:
extension Range where Bound == String.Index {
public func dump() -> String {
return "[\(lowerBound.encodedOffset)..<\(upperBound.encodedOffset)] (\(length))"
}
public var length: Int {
return upperBound.encodedOffset - lowerBound.encodedOffset
}
}
Since in Swift 5 encodedOffset is deprecated I changed implementation as below:
extension Range where Bound == String.Index {
public func dump(string: String) -> String {
let lower = lowerBound.utf16Offset(in: string)
let upper = upperBound.utf16Offset(in: string)
let result = "[\(lower)..<\(upper)] (\(upper - lower))"
return result
}
}
Test working as expected after implementation update:
class RangeTests: LogicTestCase {
func test_dump() {
let string = "Hello!"
let range = string.range(of: "ello")
Assert.equals(range?.dump(string: string), "[1..<5] (4)")
}
}
But function dump works correctly even if empty string is passed:
class RangeTests: LogicTestCase {
func test_dump() {
let string = "Hello!"
let range = string.range(of: "ello")
Assert.equals(range?.dump(string: ""), "[1..<5] (4)")
}
}
Shouldn't, for instance, call to lowerBound.utf16Offset(in: "") throw exception because we passing empty string, while range itself not empty?
UPDATE 1:
With the trick, with lowerBound.utf16Offset(in: "") mentioned above, the following two versions of sample index shifting function works identically:
extension String.Index {
// Deprecated due `encodedOffset`
public func shifting(by offset: Int) -> String.Index {
return String.Index(encodedOffset: encodedOffset + offset)
}
// Possible Swift 5 replacement to achieve index manipulation
// without reference to string.
public func shifting(by offset: Int) -> String.Index {
let newOffset = utf16Offset(in: "") + offset
let referenceString = String(repeating: " ", count: newOffset)
let result = String.Index(utf16Offset: newOffset, in: referenceString)
return result
}
}

Using MD5 hash value on an ansi string in Swift 3

I have a small function that takes a string and returns its MD5 hash value. The problem is, that it expects a UTF8 string, and I need it to calculate a hash value of a byte array encoded with iso-8859-1 (~ansi).
How can I change the following code to accept a byte array of characters, then return its hashed value?
static func md5(_ string: String) -> String {
let context = UnsafeMutablePointer<CC_MD5_CTX>.allocate(capacity: 1)
var digest = Array<UInt8>(repeating:0, count:Int(CC_MD5_DIGEST_LENGTH))
CC_MD5_Init(context)
CC_MD5_Update(context, string, CC_LONG(string.lengthOfBytes(using: String.Encoding.utf8)))
CC_MD5_Final(&digest, context)
context.deallocate(capacity: 1)
var hexString = ""
for byte in digest {
hexString += String(format:"%02x", byte)
}
return hexString
}
The tricky part is the CC_MD5_Update call. Thanks.
You can easily modify your function to take an arbitrary byte
array as argument. CC_MD5_Update is mapped to Swift as
func CC_MD5_Update(_ c: UnsafeMutablePointer<CC_MD5_CTX>!, _ data: UnsafeRawPointer!, _ len: CC_LONG) -> Int32
and you can pass an array as the UnsafeRawPointer parameter:
func md5(bytes: [UInt8]) -> String {
var context = CC_MD5_CTX()
var digest = Array<UInt8>(repeating:0, count:Int(CC_MD5_DIGEST_LENGTH))
CC_MD5_Init(&context)
CC_MD5_Update(&context, bytes, CC_LONG(bytes.count))
CC_MD5_Final(&digest, &context)
return digest.map { String(format: "%02hhx", $0) }.joined()
}
(I have also simplified it a bit.)
Alternatively, pass a Data argument:
func md5(data: Data) -> String {
var context = CC_MD5_CTX()
var digest = Array<UInt8>(repeating:0, count:Int(CC_MD5_DIGEST_LENGTH))
CC_MD5_Init(&context)
data.withUnsafeBytes {
_ = CC_MD5_Update(&context, $0, CC_LONG(data.count))
}
CC_MD5_Final(&digest, &context)
return digest.map { String(format: "%02hhx", $0) }.joined()
}
which can then be used as
let s = "foo"
if let data = s.data(using: .isoLatin1) {
let hash = md5(data: data)
print(hash)
}
Update for Swift 5:
import CommonCrypto
func md5(data: Data) -> String {
var context = CC_MD5_CTX()
var digest = Array<UInt8>(repeating:0, count:Int(CC_MD5_DIGEST_LENGTH))
CC_MD5_Init(&context)
data.withUnsafeBytes {
_ = CC_MD5_Update(&context, $0.baseAddress, CC_LONG(data.count))
}
CC_MD5_Final(&digest, &context)
return digest.map { String(format: "%02hhx", $0) }.joined()
}
If you are sure your 'string' contains only utf8 characters, call CC_MD5_Update with string.utf8 so:
CC_MD5_Update(context, string.utf8, CC_LONG(string.lengthOfBytes(using: String.Encoding.utf8)))
Strings in swift are 'interesting', this is a good read on the topic: https://oleb.net/blog/2016/08/swift-3-strings/
// requires a bridging header with this:
// #import <CommonCrypto/CommonCrypto.h>
func MD5(_ string: String) -> String? {
let length = Int(CC_MD5_DIGEST_LENGTH)
var digest = [UInt8](repeating: 0, count: length)
if let d = string.data(using: String.Encoding.utf8) {
d.withUnsafeBytes { (body: UnsafePointer<UInt8>) in
CC_MD5(body, CC_LONG(d.count), &digest)
}
}
return (0..<length).reduce("") {
$0 + String(format: "%02x", digest[$1])
}
}
Justin Answer : https://gist.github.com/jstn/787da74ab4be9d4cf3cb

how to use SHA256 with salt(some key) in swift

I found we can hash some string with CommonCrypto.
and I see some examples but they don't use salt.
how can i use the SHA256 with salt?
Complete solution for Swift 4:
extension Data {
var hexString: String {
return map { String(format: "%02hhx", $0) }.joined()
}
var sha256: Data {
var digest = [UInt8](repeating: 0, count: Int(CC_SHA256_DIGEST_LENGTH))
self.withUnsafeBytes({
_ = CC_SHA256($0, CC_LONG(self.count), &digest)
})
return Data(bytes: digest)
}
}
extension String {
func sha256(salt: String) -> Data {
return (self + salt).data(using: .utf8)!.sha256
}
}
Example:
let hash = "test".sha256(salt: "salt").hexString
Combine your indata with a salt and run the hash calculation;
func hash(input: String, salt: String) -> String {
let toHash = input + salt
// TODO: Calculate the SHA256 hash of "toHash" and return it
// return sha256(toHash)
// Return the input data and hash for now
return toHash
}
print(hash("somedata", salt: "1m8f")) // Prints "somedata1m8f"

Case insensitive Dictionary in Swift

Given a Dictionary whose Key is of type String, is there a way to access the value in a case-insensitive manner? For example:
let dict = [
"name": "John",
"location": "Chicago"
]
Is there a way to call dict["NAME"], dict["nAmE"], etc. and stil get "John"?
A cleaner approach, swift 4:
extension Dictionary where Key == String {
subscript(caseInsensitive key: Key) -> Value? {
get {
if let k = keys.first(where: { $0.caseInsensitiveCompare(key) == .orderedSame }) {
return self[k]
}
return nil
}
set {
if let k = keys.first(where: { $0.caseInsensitiveCompare(key) == .orderedSame }) {
self[k] = newValue
} else {
self[key] = newValue
}
}
}
}
// Usage:
var dict = ["name": "John"]
dict[caseInsensitive: "NAME"] = "David" // overwrites "name" value
print(dict[caseInsensitive: "name"]!) // outputs "David"
Swift support multiple subscripting so you can take advantage of that to define a case-insensitve accessor:
extension Dictionary where Key : StringLiteralConvertible {
subscript(ci key : Key) -> Value? {
get {
let searchKey = String(key).lowercaseString
for k in self.keys {
let lowerK = String(k).lowercaseString
if searchKey == lowerK {
return self[k]
}
}
return nil
}
}
}
// Usage:
let dict = [
"name": "John",
"location": "Chicago",
]
print(dict[ci: "NAME"]) // John
print(dict[ci: "lOcAtIoN"]) // Chicago
This extension is limited to Dictionary whose Key is of type String (as lowercase is meaningless with other data types). However, Swift will complain about constraining a generic type to a struct. The protocol that is closest to String is StringLiteralConvertible.
Note that if you have 2 keys whose lowercase forms are identical, there's no guarantee which one you will get back:
let dict = [
"name": "John",
"NAME": "David",
]
print(dict[ci: "name"]) // no guarantee that you will get David or John.
The existing answers are fine, but the time complexity of lookups/insertions with those strategies deteriorates from O(1) to O(N) (where N is the number of objects in the dictionary).
To retain O(1) you may want to consider the following approach:
/// Wrapper around String which uses case-insensitive implementations for Hashable
public struct CaseInsensitiveString: Hashable, LosslessStringConvertible, ExpressibleByStringLiteral {
public typealias StringLiteralType = String
private let value: String
private let caseInsensitiveValue: String
public init(stringLiteral: String) {
self.value = stringLiteral
self.caseInsensitiveValue = stringLiteral.lowercased()
}
public init?(_ description: String) {
self.init(stringLiteral: description)
}
public var hashValue: Int {
return self.caseInsensitiveValue.hashValue
}
public static func == (lhs: CaseInsensitiveString, rhs: CaseInsensitiveString) -> Bool {
return lhs.caseInsensitiveValue == rhs.caseInsensitiveValue
}
public var description: String {
return value
}
}
var dict = [CaseInsensitiveString: String]()
dict["name"] = "John"
dict["NAME"] = "David" // overwrites "name" value
print(dict["name"]!) // outputs "David"
can use Collection's first(where:) to find first lowercased match from all keys mapped lowercased, then return the value from this result.
extension Dictionary where Key == String {
func valueForKeyInsensitive<T>(key: Key) -> T? {
let foundKey = self.keys.first { $0.compare(key, options: .caseInsensitive) == .orderedSame } ?? key
return self[foundKey] as? T
}
}
first(where:) is a much efficient way to filter or iterate over the large collection
reference:
https://developer.apple.com/documentation/swift/anybidirectionalcollection/2906322-first#
https://github.com/realm/SwiftLint/blob/master/Rules.md#first-where
This should do the job with O(1) while also not allowing to add the same string with different casing (e.g. if you first insert Def it is not replaced by DEF). It also works for Substring if necessary. Note, that this solution is more memory effective, but comes at the cost at recomputing the string transformation and hash on every lookup of a string. If you need to look-up the same value frequently it might be worth to have an implementation which caches the hashValue.
struct CaseInsensitiveString<T: StringProtocol>: Hashable, Equatable, CustomStringConvertible {
var string: T
init(_ string: T) {
self.string = string
}
var description: String { get {
return string.description
}}
var hashValue: Int { get {
string.lowercased().hashValue
} }
func hash(into hasher: inout Hasher) {
hasher.combine(hashValue)
}
static func == (lhs: Self, rhs: Self) -> Bool {
return lhs.string.compare(rhs.string, options: .caseInsensitive) == .orderedSame
}
}
typealias SubstringCI = CaseInsensitiveString<String>
var codeMap = [SubstringCI: Int]()
let test = "Abc Def Ghi"
let testsub = test[test.firstIndex(of: "D")!...test.lastIndex(of: "f")!]
codeMap[SubstringCI(String(testsub))] = 1
print(codeMap.keys, codeMap[SubstringCI("Def")]!, codeMap[SubstringCI("def")]!)
codeMap[SubstringCI("DEF")] = 1
print(codeMap.keys, codeMap[SubstringCI("Def")]!, codeMap[SubstringCI("def")]!)