Este producto no es compatible con el sitio Datadog seleccionado. ().
Esta página aún no está disponible en español. Estamos trabajando en su traducción.
Si tienes alguna pregunta o comentario sobre nuestro actual proyecto de traducción, no dudes en ponerte en contacto con nosotros.

Metadata

ID: java-security/bad-hexa-concatenation

Language: Java

Severity: Warning

Category: Security

CWE: 704

Description

This rule detects improper concatenation of hexadecimal strings generated by methods like Integer.toHexString() without ensuring a fixed length for each byte representation. Such concatenation can produce inconsistent or ambiguous results because toHexString() omits leading zeros, causing the output to vary in length and potentially misrepresent the intended data.

To ensure you get a consistent hexadecimal representation get the hexadecimal representation of a value using String.format("%02x", b) or String.format("%02X", b). This guarantees consistent length and proper zero-padding for each byte.

Non-Compliant Code Examples

class Compliant2 {
  public static String getBitfieldHex(final int bitfield) {
    String hex = Integer.toHexString(bitfield & 0xFF);
  }

  public static String getBitfieldHex2() {
    int bitfield = 1;
    String hex = Integer.toHexString(foo & 0xFF);
  }
}
class NotCompliant {
    public void myMethod() {
        MessageDigest md = MessageDigest.getInstance("SHA-256");
        byte[] resultBytes = md.digest(password.getBytes("UTF-8"));

        StringBuilder stringBuilder = new StringBuilder();
        for(byte b :resultBytes) {
            stringBuilder.append(Integer.toHexString( b & 0xFF ));
        }

        return stringBuilder.toString();
    }
}

Compliant Code Examples

class NotCompliant {
    public void myMethod() {
        MessageDigest md = MessageDigest.getInstance("SHA-256");
        byte[] resultBytes = md.digest(password.getBytes("UTF-8"));

        StringBuilder stringBuilder = new StringBuilder();
        for(byte b :resultBytes) {
            stringBuilder.append( String.format( "%02X", b ) );
        }

        return stringBuilder.toString();
    }
}
https://static.datadoghq.com/static/images/logos/github_avatar.svg https://static.datadoghq.com/static/images/logos/vscode_avatar.svg jetbrains

Integraciones sin problemas. Prueba Datadog Code Security