This product is not supported for your selected Datadog site. ().
이 페이지는 아직 영어로 제공되지 않습니다. 번역 작업 중입니다.
현재 번역 프로젝트에 대한 질문이나 피드백이 있으신 경우 언제든지 연락주시기 바랍니다.

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

원활한 통합. Datadog Code Security를 경험해 보세요