_tmp_write_utf8.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. # -*- coding: ascii -*-
  2. # ASCII-safe writer: builds Chinese via unicode escapes, writes UTF-8 no BOM.
  3. from __future__ import print_function
  4. import os
  5. import sys
  6. ROOT = os.path.dirname(os.path.abspath(__file__))
  7. TARGET = os.path.join(
  8. ROOT,
  9. "fs-store",
  10. "src",
  11. "main",
  12. "java",
  13. "com",
  14. "fs",
  15. "store",
  16. "utils",
  17. "CompatiblePasswordEncoder.java",
  18. )
  19. # Chinese via unicode escapes (real chars after decode, not literal \\u in output)
  20. L10 = " * " + "\u5bc6\u7801\u7f16\u7801\u5668\uff1a\u65b0\u5bc6\u7801\u4f7f\u7528 BCrypt\uff1b\u5b58\u91cf MD5 \u5bc6\u7801\u517c\u5bb9\u6821\u9a8c\u3002"
  21. L12 = " * matches \u5185\u5bf9 MD5 \u6210\u529f\u4ec5\u8fd4\u56de true\uff0c\u4e0d\u5199\u5e93\uff08\u65e0\u7528\u6237\u4e0a\u4e0b\u6587\u65f6\u65e0\u6cd5\u5b89\u5168\u5347\u7ea7\uff09\u3002"
  22. L13 = " * \u767b\u5f55\u6210\u529f\u540e\u7531 LoginService / LoginServiceScrm \u68c0\u6d4b isLegacyMd5 \u5e76\u5347\u7ea7\u4e3a BCrypt\u3002"
  23. C_BCRYPT = " // BCrypt \u5bc6\u6587"
  24. C_MD5 = " // \u5b58\u91cf MD5 hex \u517c\u5bb9\uff08\u7b97\u6cd5\u4e0e MD5PasswordEncoder \u4e00\u81f4\uff09"
  25. J_LEGACY = " * \u5224\u65ad\u662f\u5426\u4e3a\u5386\u53f2 MD5 \u5bc6\u6587\uff08\u975e BCrypt\uff09"
  26. J_BCRYPT = " * \u5224\u65ad\u662f\u5426\u4e3a BCrypt \u5bc6\u6587\uff08$2a$ / $2b$ / $2y$\uff09"
  27. J_MD5ALG = " * \u4e0e\u73b0\u6709 MD5PasswordEncoder \u76f8\u540c\u7684 MD5 hex \u7b97\u6cd5"
  28. content = "\n".join([
  29. "package com.fs.store.utils;",
  30. "",
  31. "import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;",
  32. "import org.springframework.security.crypto.password.PasswordEncoder;",
  33. "",
  34. "import java.security.MessageDigest;",
  35. "import java.security.NoSuchAlgorithmException;",
  36. "",
  37. "/**",
  38. L10,
  39. " * <p>",
  40. L12,
  41. L13,
  42. " */",
  43. "public class CompatiblePasswordEncoder implements PasswordEncoder {",
  44. "",
  45. " private final BCryptPasswordEncoder bcryptPasswordEncoder = new BCryptPasswordEncoder();",
  46. "",
  47. " @Override",
  48. " public String encode(CharSequence rawPassword) {",
  49. " return bcryptPasswordEncoder.encode(rawPassword);",
  50. " }",
  51. "",
  52. " @Override",
  53. " public boolean matches(CharSequence rawPassword, String encodedPassword) {",
  54. " if (encodedPassword == null || rawPassword == null) {",
  55. " return false;",
  56. " }",
  57. C_BCRYPT,
  58. " if (isBcrypt(encodedPassword)) {",
  59. " return bcryptPasswordEncoder.matches(rawPassword, encodedPassword);",
  60. " }",
  61. C_MD5,
  62. " return md5Hex(rawPassword.toString()).equalsIgnoreCase(encodedPassword);",
  63. " }",
  64. "",
  65. " /**",
  66. J_LEGACY,
  67. " */",
  68. " public static boolean isLegacyMd5(String encodedPassword) {",
  69. " return encodedPassword != null && !isBcrypt(encodedPassword);",
  70. " }",
  71. "",
  72. " /**",
  73. J_BCRYPT,
  74. " */",
  75. " public static boolean isBcrypt(String encodedPassword) {",
  76. " return encodedPassword != null",
  77. " && (encodedPassword.startsWith(\"$2a$\")",
  78. " || encodedPassword.startsWith(\"$2b$\")",
  79. " || encodedPassword.startsWith(\"$2y$\"));",
  80. " }",
  81. "",
  82. " /**",
  83. J_MD5ALG,
  84. " */",
  85. " private static String md5Hex(String rawPassword) {",
  86. " try {",
  87. " MessageDigest md = MessageDigest.getInstance(\"MD5\");",
  88. " byte[] digest = md.digest(rawPassword.getBytes());",
  89. " StringBuilder sb = new StringBuilder();",
  90. " for (byte b : digest) {",
  91. " sb.append(String.format(\"%02x\", b));",
  92. " }",
  93. " return sb.toString();",
  94. " } catch (NoSuchAlgorithmException e) {",
  95. " throw new RuntimeException(\"MD5 algorithm not found\", e);",
  96. " }",
  97. " }",
  98. "}",
  99. "",
  100. ])
  101. # UTF-8 without BOM
  102. with open(TARGET, "wb") as f:
  103. f.write(content.encode("utf-8"))
  104. lines = content.split("\n")
  105. line10 = lines[9]
  106. print("LINE10=" + line10)
  107. print("HAS_CN=" + str("\u5bc6\u7801\u7f16\u7801\u5668" in line10))
  108. raw = open(TARGET, "rb").read(3)
  109. print("BOM=" + str(raw == b"\xef\xbb\xbf"))
  110. print("WROTE=" + TARGET)