value_traits.hpp 3.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. // Copyright (c) 2017-2021 Thomas Fussell
  2. //
  3. // Permission is hereby granted, free of charge, to any person obtaining a copy
  4. // of this software and associated documentation files (the "Software"), to deal
  5. // in the Software without restriction, including without limitation the rights
  6. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. // copies of the Software, and to permit persons to whom the Software is
  8. // furnished to do so, subject to the following conditions:
  9. //
  10. // The above copyright notice and this permission notice shall be included in
  11. // all copies or substantial portions of the Software.
  12. //
  13. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  19. // THE SOFTWARE
  20. //
  21. // @license: http://www.opensource.org/licenses/mit-license.php
  22. // @author: see AUTHORS file
  23. #pragma once
  24. #include <string>
  25. #include <detail/default_case.hpp>
  26. #include <detail/cryptography/hash.hpp>
  27. #include <detail/external/include_libstudxml.hpp>
  28. namespace xml {
  29. template <>
  30. struct value_traits<xlnt::detail::hash_algorithm>
  31. {
  32. static xlnt::detail::hash_algorithm parse(std::string hash_algorithm_string, const parser &)
  33. {
  34. if (hash_algorithm_string == "SHA1")
  35. return xlnt::detail::hash_algorithm::sha1;
  36. else if (hash_algorithm_string == "SHA256")
  37. return xlnt::detail::hash_algorithm::sha256;
  38. else if (hash_algorithm_string == "SHA384")
  39. return xlnt::detail::hash_algorithm::sha384;
  40. else if (hash_algorithm_string == "SHA512")
  41. return xlnt::detail::hash_algorithm::sha512;
  42. else if (hash_algorithm_string == "MD5")
  43. return xlnt::detail::hash_algorithm::md5;
  44. else if (hash_algorithm_string == "MD4")
  45. return xlnt::detail::hash_algorithm::md4;
  46. else if (hash_algorithm_string == "MD2")
  47. return xlnt::detail::hash_algorithm::md2;
  48. else if (hash_algorithm_string == "Ripemd128")
  49. return xlnt::detail::hash_algorithm::ripemd128;
  50. else if (hash_algorithm_string == "Ripemd160")
  51. return xlnt::detail::hash_algorithm::ripemd160;
  52. else if (hash_algorithm_string == "Whirlpool")
  53. return xlnt::detail::hash_algorithm::whirlpool;
  54. default_case(xlnt::detail::hash_algorithm::sha1);
  55. }
  56. static std::string serialize(xlnt::detail::hash_algorithm algorithm, const serializer &)
  57. {
  58. switch (algorithm)
  59. {
  60. case xlnt::detail::hash_algorithm::sha1:
  61. return "SHA1";
  62. case xlnt::detail::hash_algorithm::sha256:
  63. return "SHA256";
  64. case xlnt::detail::hash_algorithm::sha384:
  65. return "SHA384";
  66. case xlnt::detail::hash_algorithm::sha512:
  67. return "SHA512";
  68. case xlnt::detail::hash_algorithm::md5:
  69. return "MD5";
  70. case xlnt::detail::hash_algorithm::md4:
  71. return "MD4";
  72. case xlnt::detail::hash_algorithm::md2:
  73. return "MD2";
  74. case xlnt::detail::hash_algorithm::ripemd128:
  75. return "Ripemd128";
  76. case xlnt::detail::hash_algorithm::ripemd160:
  77. return "Ripemd160";
  78. case xlnt::detail::hash_algorithm::whirlpool:
  79. return "Whirlpool";
  80. }
  81. default_case("SHA1");
  82. }
  83. }; // struct value_traits<xlnt::detail::hash_algorithm>
  84. } // namespace xml