How to Convert String to SHA-256 & SHA-512 with Swift

The SHA (Secure Hash Algorithm) is one of a number of cryptographic hash functions. A cryptographic hash is like a signature for a text or a data file. Hash is a one way function – it cannot be decrypted back. It’s implemented in some widely used security applications and protocols, including TLS and SSL, PGP, SSH, S/MIME, IPsec, image caching. Moreover, this makes it suitable for password validation, challenge hash authentication, anti-tamper, digital signatures, etc.

SHA-256 algorithm generates an almost-unique, fixed size 256-bit (32-byte) hash. Convert value to SHA-256:

sha256(value: "value to convert")

SHA-512 is a cryptographic hash function that when applied to the provided input results in a 128-digit hexadecimal number that is highly unlikely to match the value produced for a different input. Convert value to SHA-512 only with this function:

sha512(value: "value to convert")

Previously install AfriwanLib from this Github repository, then import AfriwanLib on your class and you can use simple function above. There are still many more functions from the library that make making iOS app easier and faster.

Posted on: July 10, 2019, by :
Translate »