String+Extension.swift 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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. return data(using: String.Encoding.utf8, allowLossyConversion: true)?.bytes ?? Array(utf8)
  19. }
  20. public func md5() -> String {
  21. return bytes.md5().toHexString()
  22. }
  23. public func sha1() -> String {
  24. return bytes.sha1().toHexString()
  25. }
  26. public func sha224() -> String {
  27. return bytes.sha224().toHexString()
  28. }
  29. public func sha256() -> String {
  30. return bytes.sha256().toHexString()
  31. }
  32. public func sha384() -> String {
  33. return bytes.sha384().toHexString()
  34. }
  35. public func sha512() -> String {
  36. return bytes.sha512().toHexString()
  37. }
  38. public func sha3(_ variant: SHA3.Variant) -> String {
  39. return bytes.sha3(variant).toHexString()
  40. }
  41. public func crc32(seed: UInt32? = nil, reflect: Bool = true) -> String {
  42. return bytes.crc32(seed: seed, reflect: reflect).bytes().toHexString()
  43. }
  44. public func crc16(seed: UInt16? = nil) -> String {
  45. return bytes.crc16(seed: seed).bytes().toHexString()
  46. }
  47. /// - parameter cipher: Instance of `Cipher`
  48. /// - returns: hex string of bytes
  49. public func encrypt(cipher: Cipher) throws -> String {
  50. return try bytes.encrypt(cipher: cipher).toHexString()
  51. }
  52. /// - parameter cipher: Instance of `Cipher`
  53. /// - returns: base64 encoded string of encrypted bytes
  54. public func encryptToBase64(cipher: Cipher) throws -> String? {
  55. return try bytes.encrypt(cipher: cipher).toBase64()
  56. }
  57. // decrypt() does not make sense for String
  58. /// - parameter authenticator: Instance of `Authenticator`
  59. /// - returns: hex string of string
  60. public func authenticate<A: Authenticator>(with authenticator: A) throws -> String {
  61. return try bytes.authenticate(with: authenticator).toHexString()
  62. }
  63. }