String+Extension.swift 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. //
  2. // StringExtension.swift
  3. // CryptoSwift
  4. //
  5. // Copyright (C) 2014-2017 Marcin Krzyżanowski <marcin@krzyzanowskim.com>
  6. // This software is provided 'as-is', without any express or implied warranty.
  7. //
  8. // In no event will the authors be held liable for any damages arising from the use of this software.
  9. //
  10. // Permission is granted to anyone to use this software for any purpose,including commercial applications, and to alter it and redistribute it freely, subject to the following restrictions:
  11. //
  12. // - The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation is required.
  13. // - Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
  14. // - This notice may not be removed or altered from any source or binary distribution.
  15. //
  16. /** String extension */
  17. extension String {
  18. public func md5() -> String {
  19. return utf8.lazy.map({ $0 as UInt8 }).md5().toHexString()
  20. }
  21. public func sha1() -> String {
  22. return utf8.lazy.map({ $0 as UInt8 }).sha1().toHexString()
  23. }
  24. public func sha224() -> String {
  25. return utf8.lazy.map({ $0 as UInt8 }).sha224().toHexString()
  26. }
  27. public func sha256() -> String {
  28. return utf8.lazy.map({ $0 as UInt8 }).sha256().toHexString()
  29. }
  30. public func sha384() -> String {
  31. return utf8.lazy.map({ $0 as UInt8 }).sha384().toHexString()
  32. }
  33. public func sha512() -> String {
  34. return utf8.lazy.map({ $0 as UInt8 }).sha512().toHexString()
  35. }
  36. public func sha3(_ variant: SHA3.Variant) -> String {
  37. return utf8.lazy.map({ $0 as UInt8 }).sha3(variant).toHexString()
  38. }
  39. public func crc32(seed: UInt32? = nil, reflect: Bool = true) -> String {
  40. return utf8.lazy.map({ $0 as UInt8 }).crc32(seed: seed, reflect: reflect).bytes().toHexString()
  41. }
  42. public func crc16(seed: UInt16? = nil) -> String {
  43. return utf8.lazy.map({ $0 as UInt8 }).crc16(seed: seed).bytes().toHexString()
  44. }
  45. /// - parameter cipher: Instance of `Cipher`
  46. /// - returns: hex string of bytes
  47. public func encrypt(cipher: Cipher) throws -> String {
  48. return try Array(utf8).encrypt(cipher: cipher).toHexString()
  49. }
  50. /// - parameter cipher: Instance of `Cipher`
  51. /// - returns: base64 encoded string of encrypted bytes
  52. public func encryptToBase64(cipher: Cipher) throws -> String? {
  53. return try Array(utf8).encrypt(cipher: cipher).toBase64()
  54. }
  55. // decrypt() does not make sense for String
  56. /// - parameter authenticator: Instance of `Authenticator`
  57. /// - returns: hex string of string
  58. public func authenticate<A: Authenticator>(with authenticator: A) throws -> String {
  59. return try Array(utf8).authenticate(with: authenticator).toHexString()
  60. }
  61. }