Skip to content

Commit

Permalink
Refactoring: extrahiere Logik aus HBCIInstitute::extractKeys in eigen…
Browse files Browse the repository at this point in the history
…e Methode
  • Loading branch information
ruderphilipp committed Jan 22, 2022
1 parent 7072e1f commit a013318
Showing 1 changed file with 35 additions and 25 deletions.
60 changes: 35 additions & 25 deletions src/main/java/org/kapott/hbci/manager/HBCIInstitute.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,12 @@

package org.kapott.hbci.manager;

import java.io.UnsupportedEncodingException;
import java.math.BigInteger;
import java.security.Key;
import java.security.KeyFactory;
import java.security.NoSuchAlgorithmException;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.KeySpec;
import java.security.spec.RSAPublicKeySpec;
import java.util.Arrays;
Expand Down Expand Up @@ -112,30 +115,13 @@ void extractKeys(Properties result)
if (keyType==null)
continue;

String keyCountry=result.getProperty(head+".KeyName.KIK.country");
String keyBLZ=result.getProperty(head+".KeyName.KIK.blz");
String keyUserId=result.getProperty(head+".KeyName.userid");
String keyNum=result.getProperty(head+".KeyName.keynum");
String keyVersion=result.getProperty(head+".KeyName.keyversion");

HBCIUtils.log("found key "+
keyCountry+"_"+keyBLZ+"_"+keyUserId+"_"+keyType+"_"+
keyNum+"_"+keyVersion,
HBCIUtils.LOG_DEBUG);

byte[] keyExponent=result.getProperty(head+".PubKey.exponent").getBytes(Comm.ENCODING);
byte[] keyModulus=result.getProperty(head+".PubKey.modulus").getBytes(Comm.ENCODING);

KeyFactory fac=KeyFactory.getInstance("RSA");
KeySpec spec=new RSAPublicKeySpec(new BigInteger(+1,keyModulus),
new BigInteger(+1,keyExponent));
Key key=fac.generatePublic(spec);

if (keyType.equals("S")) {
passport.setInstSigKey(new HBCIKey(keyCountry,keyBLZ,keyUserId,keyNum,keyVersion,key));
foundChanges=true;
} else if (keyType.equals("V")) {
passport.setInstEncKey(new HBCIKey(keyCountry,keyBLZ,keyUserId,keyNum,keyVersion,key));
if (keyType.equals("S") || keyType.equals("V")) {
final HBCIKey hbciKey = getHbciKey(result, head, keyType);
if (keyType.equals("S")) {
passport.setInstSigKey(hbciKey);
} else if (keyType.equals("V")) {
passport.setInstEncKey(hbciKey);
}
foundChanges=true;
}
}
Expand All @@ -152,7 +138,31 @@ void extractKeys(Properties result)
acknowledgeNewKeys();
}
}


private HBCIKey getHbciKey(final Properties result, final String head, final String type)
throws UnsupportedEncodingException, NoSuchAlgorithmException, InvalidKeySpecException
{
String country = result.getProperty(head + ".KeyName.KIK.country");
String blz = result.getProperty(head + ".KeyName.KIK.blz");
String userId = result.getProperty(head + ".KeyName.userid");
String keyNum = result.getProperty(head + ".KeyName.keynum");
String keyVersion = result.getProperty(head + ".KeyName.keyversion");

HBCIUtils.log("found key "+
country+"_"+blz+"_"+userId+"_"+type+"_"+
keyNum+"_"+keyVersion,
HBCIUtils.LOG_DEBUG);

byte[] keyExponent = result.getProperty(head + ".PubKey.exponent").getBytes(Comm.ENCODING);
byte[] keyModulus = result.getProperty(head + ".PubKey.modulus").getBytes(Comm.ENCODING);

KeyFactory fac = KeyFactory.getInstance("RSA");
KeySpec spec = new RSAPublicKeySpec(new BigInteger(+1, keyModulus), new BigInteger(+1, keyExponent));
Key key = fac.generatePublic(spec);

return new HBCIKey(country, blz, userId, keyNum, keyVersion, key);
}

private void acknowledgeNewKeys()
{
StringBuffer answer=new StringBuffer();
Expand Down

0 comments on commit a013318

Please sign in to comment.