String+Extension.swift 2.9 KB

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