Skip to content

Commit

Permalink
return formated MRZ with new line at correct position
Browse files Browse the repository at this point in the history
  • Loading branch information
tsenger committed Nov 17, 2024
1 parent 060bde8 commit f009e74
Show file tree
Hide file tree
Showing 14 changed files with 180 additions and 59 deletions.
3 changes: 3 additions & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 13 additions & 0 deletions .idea/compiler.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions .idea/encodings.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 20 additions & 0 deletions .idea/jarRepositories.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@

<groupId>de.tsenger</groupId>
<artifactId>vdstools</artifactId>
<version>0.6.0</version>
<version>0.6.1</version>
<name>Visible Digital Seal Tools Java library</name>
<description>A Java library to encode/sign and decode/verify Visible Digital Seals</description>
<packaging>jar</packaging>
<url>https://tsenger.de</url>

<organization>
<name>tsenger.de</name>
<url>https://tsenger.de</url>
<url>https://www.tsenger.de</url>
</organization>

<licenses>
Expand Down Expand Up @@ -45,6 +45,8 @@
<version>3.13.0</version>
<configuration>
<showDeprecation>true</showDeprecation>
<source>11</source>
<target>11</target>
</configuration>
</plugin>
<plugin>
Expand Down
52 changes: 29 additions & 23 deletions src/main/java/de/tsenger/vdstools/FeatureConverter.java
Original file line number Diff line number Diff line change
@@ -1,24 +1,17 @@
package de.tsenger.vdstools;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import de.tsenger.vdstools.vds.dto.FeaturesDto;
import de.tsenger.vdstools.vds.dto.SealDto;
import org.tinylog.Logger;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.lang.reflect.Type;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;

import org.tinylog.Logger;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import de.tsenger.vdstools.vds.dto.FeaturesDto;
import de.tsenger.vdstools.vds.dto.SealDto;
import java.util.*;

public class FeatureConverter {

Expand Down Expand Up @@ -56,10 +49,11 @@ public FeatureConverter(InputStream is) {
}

public Set<String> getAvailableVdsTypes() {
return new TreeSet<String>(vdsTypes.keySet());
return new TreeSet<>(vdsTypes.keySet());
}

public int getDocumentRef(String vdsType) {
if (vdsTypes.get(vdsType) == null) throw new IllegalArgumentException("Could find seal type "+vdsType+" in "+DEFAULT_SEAL_CODINGS);
return vdsTypes.get(vdsType);
}

Expand All @@ -83,6 +77,22 @@ public String getFeatureName(String vdsType, DerTlv derTlv) {
return getFeatureName(sealDto, derTlv.getTag());
}

public int getFeatureLength(String vdsType, byte tag) {
if (!vdsTypes.containsKey(vdsType)) {
Logger.warn("No seal type with name '" + vdsType + "' was found.");
return -1;
}
SealDto sealDto = getSealDto(vdsType);
if (sealDto == null) {
return -1;
}
FeaturesDto featureDto = getFeatureDto(sealDto, tag);
if(featureDto == null) {
return -1;
}
return featureDto.decodedLength;
}

public byte getTag(String vdsType, String feature) {
if (!vdsTypes.containsKey(vdsType)) {
Logger.warn("No VdsSeal type with name '" + vdsType + "' was found.");
Expand Down Expand Up @@ -132,7 +142,7 @@ private <T> DerTlv encodeFeature(SealDto sealDto, String feature, T inputValue)
throw new IllegalArgumentException("VdsType: " + sealDto.documentType + " has no Feature " + feature);
}
String coding = getCoding(sealDto, feature);
byte[] value = null;
byte[] value;
switch (coding) {
case "C40":
String valueStr = ((String) inputValue).replaceAll("\r", "").replaceAll("\n", "");
Expand All @@ -142,12 +152,8 @@ private <T> DerTlv encodeFeature(SealDto sealDto, String feature, T inputValue)
value = (byte[]) inputValue;
break;
case "Utf8String":
try {
value = ((String) inputValue).getBytes("UTF-8");
} catch (UnsupportedEncodingException e) {
Logger.error("Couldn't encode String " + (String) inputValue + " to UTF-8 bytes: " + e.getMessage());
}
break;
value = ((String) inputValue).getBytes(StandardCharsets.UTF_8);
break;
default:
value = (byte[]) inputValue;
}
Expand All @@ -162,7 +168,7 @@ private <T> T decodeFeature(SealDto sealDto, DerTlv derTlv) {
case "C40":
String featureValue = DataParser.decodeC40(derTlv.getValue());
String featureName = getFeatureName(sealDto, tag);
if (featureName.startsWith("MRZ")) {
if (featureName.startsWith("MRZ")) {
featureValue = featureValue.replace(' ', '<');
}
return (T) featureValue;
Expand Down
20 changes: 10 additions & 10 deletions src/main/java/de/tsenger/vdstools/vds/VdsMessage.java
Original file line number Diff line number Diff line change
@@ -1,18 +1,13 @@
package de.tsenger.vdstools.vds;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;

import org.tinylog.Logger;

import de.tsenger.vdstools.DataEncoder;
import de.tsenger.vdstools.DataParser;
import de.tsenger.vdstools.DerTlv;
import org.tinylog.Logger;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.*;

public class VdsMessage {

Expand Down Expand Up @@ -76,6 +71,11 @@ public Optional<Feature> getFeature(String feature) {
}
}
}
if (value!=null && (feature.equals("MRZ") || feature.equals("MRZ_MRVA") || feature.equals("MRZ_MRVB"))) {
int mrzLength = DataEncoder.getFeatureEncoder().getFeatureLength(vdsType, tag);
String newMrz = String.format("%1$-"+mrzLength+"s", value).replace(' ', '<');
value = newMrz.substring(0, mrzLength / 2) + "\n" + newMrz.substring(mrzLength / 2);
}
return Optional.ofNullable(value != null ? new Feature(value) : null);
}

Expand Down
1 change: 1 addition & 0 deletions src/main/java/de/tsenger/vdstools/vds/dto/FeaturesDto.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ public class FeaturesDto {
public String name;
public int tag;
public String coding;
public int decodedLength;
public boolean required;
public int minLength;
public int maxLength;
Expand Down
Loading

0 comments on commit f009e74

Please sign in to comment.