String+Extension.swift 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. //
  2. // CryptoSwift
  3. //
  4. // Copyright (C) 2014-2017 Marcin Krzyżanowski <marcin@krzyzanowskim.com>
  5. // This software is provided 'as-is', without any express or implied warranty.
  6. //
  7. // In no event will the authors be held liable for any damages arising from the use of this software.
  8. //
  9. // 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:
  10. //
  11. // - 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.
  12. // - Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
  13. // - This notice may not be removed or altered from any source or binary distribution.
  14. //
  15. /** String extension */
  16. extension String {
  17. public var bytes: Array<UInt8> {
  18. data(using: String.Encoding.utf8, allowLossyConversion: true)?.bytes ?? Array(utf8)
  19. }
  20. public func md5() -> String {
  21. self.bytes.md5().toHexString()
  22. }
  23. public func sha1() -> String {
  24. self.bytes.sha1().toHexString()
  25. }
  26. public func sha224() -> String {
  27. self.bytes.sha224().toHexString()
  28. }
  29. public func sha256() -> String {
  30. self.bytes.sha256().toHexString()
  31. }
  32. public func sha384() -> String {
  33. self.bytes.sha384().toHexString()
  34. }
  35. public func sha512() -> String {
  36. self.bytes.sha512().toHexString()
  37. }
  38. public func sha3(_ variant: SHA3.Variant) -> String {
  39. self.bytes.sha3(variant).toHexString()
  40. }
  41. public func crc32(seed: UInt32? = nil, reflect: Bool = true) -> String {
  42. self.bytes.crc32(seed: seed, reflect: reflect).bytes().toHexString()
  43. }
  44. public func crc32c(seed: UInt32? = nil, reflect: Bool = true) -> String {
  45. self.bytes.crc32c(seed: seed, reflect: reflect).bytes().toHexString()
  46. }
  47. public func crc16(seed: UInt16? = nil) -> String {
  48. self.bytes.crc16(seed: seed).bytes().toHexString()
  49. }
  50. /// - parameter cipher: Instance of `Cipher`
  51. /// - returns: hex string of bytes
  52. public func encrypt(cipher: Cipher) throws -> String {
  53. try self.bytes.encrypt(cipher: cipher).toHexString()
  54. }
  55. /// - parameter cipher: Instance of `Cipher`
  56. /// - returns: base64 encoded string of encrypted bytes
  57. public func encryptToBase64(cipher: Cipher) throws -> String? {
  58. try self.bytes.encrypt(cipher: cipher).toBase64()
  59. }
  60. // decrypt() does not make sense for String
  61. /// - parameter authenticator: Instance of `Authenticator`
  62. /// - returns: hex string of string
  63. public func authenticate<A: Authenticator>(with authenticator: A) throws -> String {
  64. try self.bytes.authenticate(with: authenticator).toHexString()
  65. }
  66. }