Doubts about formatting hexadecimal numbers in java for use in MD5

Asked

Viewed 113 times

4

I would like to know why the author of the article put this part:

senha = String.format("%1$032X", i); 

I was curious how he got this string: "%1$032X"

Follow the full code:

import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class MD5 {

    public static void main(String[] args)
    {

        String senha = "photu5678";
        MessageDigest mensagem;

        try
        {
            mensagem = MessageDigest.getInstance("MD5");
            mensagem.update(senha.getBytes(), 0, senha.length());
            BigInteger i = new BigInteger(1, mensagem.digest());

            // Formatando o resultado em uma cadeia de 32 caracteres, completando com 0 caso falte 
            senha = String.format("%1$032X", i); 

            System.out.println("MD5: " + senha); 
        } 

        catch (NoSuchAlgorithmException e) 
        { 
            e.printStackTrace(); 
        } 
    }
} 

1 answer

4


The answer is in the class Formatter which is used by the method String.format:

  • The format specifiers for general, Character, and Numeric types have the following syntax:

      %[argument_index$][flags][width][.precision]conversion
    

    The optional argument_index is a decimal integer indicating the position of the argument in the argument list. The first argument is referenced by "1$", the Second by "2$", etc..

    The optional flags is a set of characters that Modify the output format. The set of Valid flags depends on the Conversion.

    The optional width is a Positive decimal integer indicating the minimum number of characters to be Written to the output.

    The optional Precision is a non-negative decimal integer usually used to restrict the number of characters. The specific behavior depends on the Conversion.

    The required Conversion A Character indicating how the argument should be Formatted. The set of Valid conversions for a Given argument depends on the argument’s data type.

Translating into Portuguese:

  • Format specifiers for general, character and numeric types have the following syntax:

      %[argument_index$][flags][width][.precision]conversion
    

    The argument_index optional is a decimal integer indicating the position of the argument in the argument list. The first argument is referenced by "1$", the second per "2$", etc..

    The flags optional is a character set that modifies the output format. The valid flags set depends on the Conversion.

    The width optional is a positive decimal integer indicating the minimum number of characters to be written on output.

    The Precision optional is a non-negative decimal integer usually used to restrict the number of characters. The specific behavior depends on the Conversion.

    The Conversion required is the character indicating how the argument will be formatted. The set of valid conversions for a given argument depends on the data type of the argument.

In the case of Conversion, this is what the class Formatter specifies, among other things:

'x', 'X' The result is Formatted as a hexadecimal integer

Translating:

'x', 'X' The result is formatted as a hexadecimal integer

According to the documentation, conversions uppercase output will produce uppercase and lowercase output.

As to the flags:

'0' The result will be zero-Padded

Translating:

'0' The result will be completed with zeros [left]

That is, in the case of "%1$032X", we have the following:

  • % indicates that the following text is a format specifier.
  • 1$ is the argument_index that references the first argument of the argument list (will be the contents of the variable i).
  • 0 is the flags which specifies that the number must be completed with zeros on the left.
  • 32 is the width, that is, the length of the number. That is, 32 characters.
  • X is the Conversion, specifying that the number shall be expressed in hexadecimal digits, using capital letters for the digits A-F.
  • 1

    Now yes. Thank you.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.