Skip to content

Commit

Permalink
chore: fix checkstyle warnings
Browse files Browse the repository at this point in the history
Signed-off-by: Josef Andersson <[email protected]>
  • Loading branch information
janderssonse committed Feb 20, 2025
1 parent 45813ce commit 8ab6ad2
Show file tree
Hide file tree
Showing 25 changed files with 313 additions and 226 deletions.
10 changes: 5 additions & 5 deletions development/code_quality.sh
Original file line number Diff line number Diff line change
Expand Up @@ -87,10 +87,10 @@ commit() {
printf '\n\n'
}

format() {
print_header 'FORMATTING (PRETTIER)'
mvn formatter:format "${MAVEN_CLI_OPTS[@]}" -DskipTests
store_exit_code "$?" "Format" "${MISSING} ${RED}Format check failed, see logs (std out) and fix problems.${NC}\n" "${GREEN}${CHECKMARK}${CHECKMARK} Format check passed${NC}\n"
verify() {
print_header 'MVN VERIFY'
mvn clean verify "${MAVEN_CLI_OPTS[@]}"
store_exit_code "$?" "Verify" "${MISSING} ${RED}Verify check failed, see logs (std out) and fix problems.${NC}\n" "${GREEN}${CHECKMARK}${CHECKMARK} Verify check passed${NC}\n"
printf '\n\n'
}

Expand Down Expand Up @@ -130,7 +130,7 @@ is_command_available 'sed' ''

lint
commit
format
verify
license

check_exit_codes
2 changes: 1 addition & 1 deletion development/megalinter.yml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,6 @@
BASH_SHELLCHECK_FILE_EXTENSIONS: [.sh]
JAVA_CHECKSTYLE_FILTER_REGEX_INCLUDE: src/main
JAVA_CHECKSTYLE_CONFIG_FILE: development/checkstyle/google_checks.xml
JAVA_PMD_CONFIG_FILE: development/pmd_default_java_mod.xml
JAVA_PMD_CONFIG_FILE: development/pmd/pmd_default_java_mod.xml
REPOSITORY_GITLEAKS_ARGUMENTS: --log-opts="main..HEAD"
# LOG_LEVEL: DEBUG # will show you the exact command run
File renamed without changes.
2 changes: 1 addition & 1 deletion docs/DEVELOPMENT.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ mvn clean verify

1. Install a Checkstyle plugin - [Checkstyle For Java](https://marketplace.visualstudio.com/items?itemName=shengchen.vscode-checkstyle)

2. Open user settings - settings.json (for example with Ctrl+Shift+P -> Preferences: User Settings (JSON)) and make sure you have the following settings:
2. Open workspace settings - settings.json (for example with Ctrl+Shift+P -> Preferences: Workspace Settings (JSON)) and make sure you have the following settings:
```json
"[java]": {
"editor.defaultFormatter": "redhat.java",
Expand Down
1 change: 0 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,6 @@
<configuration>
<configFile>${project.basedir}/development/formatting/eclipse-java-google-style.xml</configFile>
<lineEnding>LF</lineEnding>
<verbose>true</verbose> <!-- Add this for more logging -->
<compilerSource>21</compilerSource>
<compilerCompliance>21</compilerCompliance>
<compilerTargetPlatform>21</compilerTargetPlatform>
Expand Down
35 changes: 22 additions & 13 deletions src/main/java/se/digg/cose/ASN1.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,9 @@ public String toString() {

private static String bytesToHex(byte[] bytes) {
StringBuilder sb = new StringBuilder(bytes.length * 2);
for (byte b : bytes)
for (byte b : bytes) {
sb.append(String.format("%02x", b & 0xff));
}
return sb.toString();
}

Expand Down Expand Up @@ -165,10 +166,10 @@ private static String tagToString(int tag) {
*/
@SuppressWarnings("PMD")
public static boolean isEdXOid(byte[] oid) {
return (Arrays.equals(oid, Oid_Ed25519) ||
Arrays.equals(oid, Oid_Ed448) ||
Arrays.equals(oid, Oid_X25519) ||
Arrays.equals(oid, Oid_X448));
return (Arrays.equals(oid, Oid_Ed25519)
|| Arrays.equals(oid, Oid_Ed448)
|| Arrays.equals(oid, Oid_X25519)
|| Arrays.equals(oid, Oid_X448));
}

/**
Expand Down Expand Up @@ -259,23 +260,28 @@ public static byte[] EncodeEcPrivateKey(
public static ArrayList<TagValue> DecodeSubjectPublicKeyInfo(byte[] encoding)
throws CoseException {
TagValue spki = DecodeCompound(0, encoding);
if (spki.tag != 0x30)
if (spki.tag != 0x30) {
throw new CoseException("Invalid SPKI");
}
ArrayList<TagValue> tvl = spki.list;
if (tvl.size() != 2)
if (tvl.size() != 2) {
throw new CoseException("Invalid SPKI");
}

if (tvl.get(0).tag != 0x30)
if (tvl.get(0).tag != 0x30) {
throw new CoseException("Invalid SPKI");
}
if (tvl.get(0).list.isEmpty() || tvl.get(0).list.size() > 2) {
throw new CoseException("Invalid SPKI");
}
if (tvl.get(0).list.get(0).tag != 6)
if (tvl.get(0).list.get(0).tag != 6) {
throw new CoseException(
"Invalid SPKI");
}
// tvl.get(0).list.get(1).tag is an ANY so needs to be checked elsewhere
if (tvl.get(1).tag != 3)
if (tvl.get(1).tag != 3) {
throw new CoseException("Invalid SPKI");
}

return tvl;
}
Expand Down Expand Up @@ -303,9 +309,10 @@ public static TagValue DecodeCompound(int offset, byte[] encoding)

// We only decode objects which are compound objects. That means that this bit must be set

if ((encoding[offset] & 0x20) != 0x20)
if ((encoding[offset] & 0x20) != 0x20) {
throw new CoseException(
"Invalid structure");
}
int[] l = DecodeLength(offset + 1, encoding);
int sequenceLength = l[1];
if (offset + sequenceLength > encoding.length)
Expand Down Expand Up @@ -360,8 +367,9 @@ public static TagValue DecodeSimple(int offset, byte[] encoding)
ArrayList<TagValue> result = new ArrayList<TagValue>();
int retTag = encoding[offset];

if (encoding[offset] != 0x04)
if (encoding[offset] != 0x04) {
throw new CoseException("Invalid structure");
}
int[] l = DecodeLength(offset + 1, encoding);

int sequenceLength = l[1];
Expand Down Expand Up @@ -523,9 +531,10 @@ public static ArrayList<TagValue> DecodePKCS8EC(ArrayList<TagValue> pkcs8)
ArrayList<TagValue> pkdl = pkd.list;
if (pkd.tag != 0x30)
throw new CoseException("Invalid ECPrivateKey");
if (pkdl.size() < 2 || pkdl.size() > 4)
if (pkdl.size() < 2 || pkdl.size() > 4) {
throw new CoseException(
"Invalid ECPrivateKey");
}

if (pkdl.get(0).tag != 2 && pkcs8.get(0).value[0] != 1) {
throw new CoseException("Invalid ECPrivateKey");
Expand Down
6 changes: 4 additions & 2 deletions src/main/java/se/digg/cose/AlgorithmID.java
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,13 @@ public enum AlgorithmID {
}

public static AlgorithmID FromCBOR(CBORObject obj) throws CoseException {
if (obj == null)
if (obj == null) {
throw new CoseException("No Algorithm Specified");
}
for (AlgorithmID alg : values()) {
if (obj.equals(alg.value))
if (obj.equals(alg.value)) {
return alg;
}
}
throw new CoseException("Unknown Algorithm Specified");
}
Expand Down
20 changes: 12 additions & 8 deletions src/main/java/se/digg/cose/Attribute.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ public class Attribute {
* Holder for the external data object that is authenticated as part of the message
*/
protected byte[] externalData = new byte[0];

/**
* Used to place an attribute in the protected attribute map Attributes placed in this map are
* part of the integrity check if the cryptographic algorithm supports authenticated data.
Expand Down Expand Up @@ -112,15 +111,16 @@ public class Attribute {
public void addAttribute(CBORObject label, CBORObject value, int where)
throws CoseException {
removeAttribute(label);
if ((label.getType() != CBORType.Integer) &&
(label.getType() != CBORType.TextString)) {
if ((label.getType() != CBORType.Integer)
&& (label.getType() != CBORType.TextString)) {
throw new CoseException("Labels must be integers or strings");
}
switch (where) {
case PROTECTED:
if (rgbProtected != null)
if (rgbProtected != null) {
throw new CoseException(
"Cannot modify protected attribute if signature has been computed");
}
objProtected.Add(label, value);
break;
case UNPROTECTED:
Expand Down Expand Up @@ -278,13 +278,16 @@ public CBORObject findAttribute(CBORObject label) {
* @return - CBORObject with the value if found; otherwise null
*/
public CBORObject findAttribute(CBORObject label, int where) {
if (((where & PROTECTED) == PROTECTED) && objProtected.ContainsKey(label))
if (((where & PROTECTED) == PROTECTED) && objProtected.ContainsKey(label)) {
return objProtected.get(label);
}
if (((where & UNPROTECTED) == UNPROTECTED) &&
objUnprotected.ContainsKey(label))
objUnprotected.ContainsKey(label)) {
return objUnprotected.get(label);
if (((where & DO_NOT_SEND) == DO_NOT_SEND) && objDontSend.ContainsKey(label))
}
if (((where & DO_NOT_SEND) == DO_NOT_SEND) && objDontSend.ContainsKey(label)) {
return objDontSend.get(label);
}
return null;
}

Expand Down Expand Up @@ -382,8 +385,9 @@ public byte[] getExternal() {
* @param rgbData - data to be authenticated
*/
public void setExternal(byte[] rgbData) {
if (rgbData == null)
if (rgbData == null) {
rgbData = new byte[0];
}
externalData = rgbData;
}
}
Loading

0 comments on commit 8ab6ad2

Please sign in to comment.