Skip to content

Commit

Permalink
Merge pull request jpos#597 from fgonzal/master
Browse files Browse the repository at this point in the history
Make `KeyUsage` serializable.
  • Loading branch information
ar authored Jun 5, 2024
2 parents e38fa15 + b7bf1a1 commit 4991335
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 7 deletions.
9 changes: 6 additions & 3 deletions jpos/src/main/java/org/jpos/security/KeyUsage.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* jPOS Project [http://jpos.org]
* Copyright (C) 2000-2023 jPOS Software SRL
* Copyright (C) 2000-2024 jPOS Software SRL
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
Expand All @@ -18,6 +18,7 @@

package org.jpos.security;

import java.io.Serializable;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.Map;
Expand All @@ -27,9 +28,11 @@
/**
* Defines the primary usage of the key contained in the key block.
* <p>
* Each value repesents bytes 5-6 of the Keyblok Header.
* Each value repesents bytes 5-6 of the keyblock header.
*/
public class KeyUsage {
public class KeyUsage implements Serializable {

private static final long serialVersionUID = -5504819939017756749L;

protected static final Map<String, KeyUsage> TR31MAP = new LinkedHashMap<>();

Expand Down
33 changes: 29 additions & 4 deletions jpos/src/test/java/org/jpos/security/SecureKeyBlockTest.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* jPOS Project [http://jpos.org]
* Copyright (C) 2000-2023 jPOS Software SRL
* Copyright (C) 2000-2024 jPOS Software SRL
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
Expand All @@ -18,20 +18,23 @@

package org.jpos.security;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.PrintStream;
import java.util.Arrays;
import java.util.LinkedHashMap;
import java.util.Map;

import org.jpos.iso.ISOUtil;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;

import static org.junit.jupiter.api.Assertions.*;

/**
*
*/
public class SecureKeyBlockTest {

private static final String NL = System.getProperty("line.separator");
Expand Down Expand Up @@ -227,4 +230,26 @@ public void testDumpWithNameAndOptHeader() {
assertEquals(sb.toString(), os.toString());
}

@Test
public void testSerialization() throws IOException, ClassNotFoundException {
String tr31 = "C0088P0TN00E000054CF02CF89CCC36897E3D4AC22D7BBECA1CBD08296A315A47228274A2FAE03EA699AF35F";
SecureKeyBlock key = SecureKeyBlockBuilder.newBuilder().build(tr31);
key.dump(System.out, " ");
// Serialize instance.
Object obj = key;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream os = new ObjectOutputStream(baos);
os.writeObject(obj); // This call errors out if 'obj' is not serializable.
byte[] img = baos.toByteArray();
// Deserialize.
ByteArrayInputStream bais = new ByteArrayInputStream(img);
ObjectInputStream is = new ObjectInputStream(bais);
SecureKeyBlock key2 = (SecureKeyBlock) is.readObject();
key2.dump(System.out, " ");
assertEquals(key.getKeyName(), key2.getKeyName());
assertEquals(key.getModeOfUse(), key2.getModeOfUse());
assertEquals(key.getKeyUsage().getCode(), key2.getKeyUsage().getCode());
assertEquals(key.getAlgorithm().getCode(), key2.getAlgorithm().getCode());
assertTrue(Arrays.equals(key.getKeyBytes(), key2.getKeyBytes()));
}
}

0 comments on commit 4991335

Please sign in to comment.