Comparing two strings that are the same are apperenty NOT the same.

Discussion in 'Plugin Development' started by Cirno, Mar 9, 2012.

Thread Status:
Not open for further replies.
  1. Offline

    Cirno

    19:19:13 [SEVERE] Cirno32c875860e049e6c77baedad979df804 != Cirno32c875860e049e6c
    77baedad979df804

    Here's them line by line.
    Cirno32c875860e049e6c77baedad979df804
    Cirno32c875860e049e6c77baedad979df804

    I see no difference. Code I'm using to compare them both.
    if(admins.getString("admins." + sender.getName() + "." + arg[1]).equals("Cirno" + EncryptIt(arg[0],arg[1]))){

    And here's my custom config file (admins) stuff:
    Code:
    admins:
      Evangon:
        MD5: Cirno32c875860e049e6c77baedad979df804
        SHA-512: Cirno72356f7e292f3da69832467a30353ee09f038aca8ad5d191a46aa0b93a47fe0d3ebcb19185869cff54408ff40f5d2ab17bc0812853a2f3bc2b8ef285110a8148
    
    P.S: If you really really want to know what the MD5 and SHA-512 decrypted are, it's "LOLHAI".
     
  2. Offline

    hpdvs2

    check the lengths. Perhaps there is a space or a null character in the string, that you wouldn't see from a log by itself.
     
  3. Offline

    Cirno

    08:10:36 [SEVERE] Cirno32c875860e049e6c77baedad979df804(37) != Cirno32c875860e04
    9e6c77baedad979df804(37)

    Apparently, they match the length.
     
  4. Offline

    SirTyler

    Are you maybe doing == instead of .equal?
     
  5. Offline

    Njol

     
  6. Offline

    SirTyler

    Ahh missed that xP
    well I'm out of idea for it then.
     
  7. Offline

    Njol

    Can you post the whole code please? I know it will likely be of no use, but the line you posted is correct.
     
  8. Offline

    masteroftime

    Maybe the two strings have a different encoding?
     
  9. Offline

    Cirno

    Code:JAVA
    1.  
    2. package root;
    3.  
    4. import java.io.File;
    5. import java.io.IOException;
    6. import java.math.BigInteger;
    7. import java.security.MessageDigest;
    8. import java.security.NoSuchAlgorithmException;
    9. import java.util.logging.Logger;
    10.  
    11. import org.bukkit.Server;
    12. import org.bukkit.command.Command;
    13. import org.bukkit.command.CommandSender;
    14. import org.bukkit.configuration.file.FileConfiguration;
    15. import org.bukkit.configuration.file.YamlConfiguration;
    16. import org.bukkit.entity.Player;
    17. import org.bukkit.plugin.java.JavaPlugin;
    18.  
    19. public class main extends JavaPlugin {
    20. Server server = getServer();
    21.  
    22. // Note to self: DO NOT DELETE customConfigFile AS IT IS THE ONLY WAY TO ACCESS THE FILE IN A RAW WAY.
    23.  
    24. private FileConfiguration admins = null;
    25. private File customConfigFile = null;
    26.  
    27. public void onEnable(){
    28. reloadCustomConfig();
    29. }
    30.  
    31. public void reloadCustomConfig() {
    32. if (customConfigFile == null) {
    33. customConfigFile = new File(getDataFolder(), "admins.yml");
    34. }
    35. admins = YamlConfiguration.loadConfiguration(customConfigFile);
    36.  
    37. }
    38.  
    39.  
    40. public void onDisable(){
    41.  
    42. }
    43.  
    44. public static String EncryptIt(String input, String algorithm) {
    45. String hashtext = "";
    46. try {
    47. MessageDigest md = MessageDigest.getInstance(algorithm);
    48. byte[] messageDigest = md.digest(input.getBytes());
    49. BigInteger number = new BigInteger(1, messageDigest);
    50. hashtext = number.toString(16);
    51. while (hashtext.length() < 32) {
    52. hashtext = "0" + hashtext;
    53. }
    54.  
    55. }
    56. System.out.print("Something or someone has tried to encrypt with a non-existent algorithm.");
    57. System.out.print("Algorithm: " + algorithm);
    58. }
    59. hashtext = "Cirno" + hashtext;
    60. return hashtext;
    61. }
    62.  
    63.  
    64. public boolean onCommand(CommandSender sender, Command command, String label, String[] arg) {
    65. if(command.getName().equalsIgnoreCase("adminlogin")){
    66. if(arg == null){
    67. sender.sendMessage("How to login using AdminSecurity:");
    68. sender.sendMessage("/adminlogin <password> [encryption type]");
    69. sender.sendMessage("Encryption Types:");
    70. sender.sendMessage("MD5, SHA-512");
    71. }
    72. if(admins.get("admins." + sender.getName()) != null){
    73. if(String.valueOf(admins.getString("admins." + sender.getName()) + "." + arg[1]) == (String.valueOf("Cirno" + EncryptIt(arg[0],arg[1])))){
    74. sender.sendMessage("Yay! You logged in!");
    75. return true;
    76. } else {
    77. sender.sendMessage("NOPE.");
    78. Logger.getLogger("Minecraft").severe(String.valueOf(EncryptIt(arg[0],arg[1])) + "(" + EncryptIt(arg[0],arg[1]).length() + ")" + " != " + String.valueOf(admins.getString("admins." + sender.getName() + "." + arg[1])) + "(" + admins.getString("admins." + sender.getName() + "." + arg[1]).length() + ")");
    79. return false;
    80. }
    81. }
    82. } else if(command.getName().equalsIgnoreCase("encryptit")){
    83. if(arg == null){
    84. if(arg == null){
    85. sender.sendMessage("How to login using AdminSecurity:");
    86. sender.sendMessage("/encryptit <phrase> [encryption type]");
    87. sender.sendMessage("Encryption Types:");
    88. sender.sendMessage("MD5, MD4");
    89. }
    90. }
    91. sender.sendMessage(EncryptIt(arg[0], arg[1]));
    92. System.out.print(sender.getName() + " has encrypted a phrase into " + arg[1] );
    93. System.out.print("Hash String: " + EncryptIt(arg[0], arg[1]));
    94. System.out.print("Original: " + arg[0]);
    95. } else if(command.getName().equalsIgnoreCase("changepw")){
    96. if(sender instanceof Player == false){
    97. if(arg[0] == null || arg[1] == null || arg[2] == null){
    98. sender.sendMessage("How to use ChangePW:");
    99. sender.sendMessage("/changepw <player> <password> <algorithm>");
    100. } else {
    101. admins.set("admins." + arg[0] + "." + arg[2], EncryptIt(arg[1], arg[2]));
    102. try {
    103. admins.save(customConfigFile);
    104. } catch (IOException e) {
    105. e.printStackTrace();
    106. }
    107. }
    108. }
    109. }
    110. return false;
    111. }
    112. }
    113.  


    I am aware that I need to clean this up a bit.
     
  10. Offline

    nisovin

    You're prepending "Cirno" to the string twice. Your message in the "else" statement is not accounting for the fact that you have this error, thus you haven't found it.
     
  11. Offline

    Cirno

    What an idiot I am... Thanks.
     
Thread Status:
Not open for further replies.

Share This Page