String+Extension.swift 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. //
  2. // CryptoSwift
  3. //
  4. // Copyright (C) 2014-2022 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. @inlinable
  18. public var bytes: Array<UInt8> {
  19. data(using: String.Encoding.utf8, allowLossyConversion: true)?.bytes ?? Array(utf8)
  20. }
  21. @inlinable
  22. public func md5() -> String {
  23. self.bytes.md5().toHexString()
  24. }
  25. @inlinable
  26. public func sha1() -> String {
  27. self.bytes.sha1().toHexString()
  28. }
  29. @inlinable
  30. public func sha224() -> String {
  31. self.bytes.sha224().toHexString()
  32. }
  33. @inlinable
  34. public func sha256() -> String {
  35. self.bytes.sha256().toHexString()
  36. }
  37. @inlinable
  38. public func sha384() -> String {
  39. self.bytes.sha384().toHexString()
  40. }
  41. @inlinable
  42. public func sha512() -> String {
  43. self.bytes.sha512().toHexString()
  44. }
  45. @inlinable
  46. public func sha3(_ variant: SHA3.Variant) -> String {
  47. self.bytes.sha3(variant).toHexString()
  48. }
  49. @inlinable
  50. public func crc32(seed: UInt32? = nil, reflect: Bool = true) -> String {
  51. self.bytes.crc32(seed: seed, reflect: reflect).bytes().toHexString()
  52. }
  53. @inlinable
  54. public func crc32c(seed: UInt32? = nil, reflect: Bool = true) -> String {
  55. self.bytes.crc32c(seed: seed, reflect: reflect).bytes().toHexString()
  56. }
  57. @inlinable
  58. public func crc16(seed: UInt16? = nil) -> String {
  59. self.bytes.crc16(seed: seed).bytes().toHexString()
  60. }
  61. /// - parameter cipher: Instance of `Cipher`
  62. /// - returns: hex string of bytes
  63. @inlinable
  64. public func encrypt(cipher: Cipher) throws -> String {
  65. try self.bytes.encrypt(cipher: cipher).toHexString()
  66. }
  67. /// - parameter cipher: Instance of `Cipher`
  68. /// - returns: base64 encoded string of encrypted bytes
  69. @inlinable
  70. public func encryptToBase64(cipher: Cipher) throws -> String {
  71. try self.bytes.encrypt(cipher: cipher).toBase64()
  72. }
  73. // decrypt() does not make sense for String
  74. /// - parameter authenticator: Instance of `Authenticator`
  75. /// - returns: hex string of string
  76. @inlinable
  77. public func authenticate<A: Authenticator>(with authenticator: A) throws -> String {
  78. try self.bytes.authenticate(with: authenticator).toHexString()
  79. }
  80. }