mirror of
https://codeberg.org/Mercury-IM/Smack
synced 2024-11-25 15:52:06 +01:00
Improve OX's PubkeyElement
there is no reason why we want to store the bytes of the base64 encoded string. Simply store the string itself. Also add a convenience method to get the decoded bytes of the PubKey.
This commit is contained in:
parent
34f1c2b79e
commit
a6a1142255
4 changed files with 40 additions and 29 deletions
|
@ -636,7 +636,8 @@ public final class OpenPgpManager extends Manager {
|
|||
* @return {@link PubkeyElement} containing the key
|
||||
*/
|
||||
private static PubkeyElement createPubkeyElement(byte[] bytes, Date date) {
|
||||
return new PubkeyElement(new PubkeyElement.PubkeyDataElement(Base64.encode(bytes)), date);
|
||||
String base64EncodedOpenPgpPubKey = Base64.encodeToString(bytes);
|
||||
return new PubkeyElement(new PubkeyElement.PubkeyDataElement(base64EncodedOpenPgpPubKey), date);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -16,12 +16,12 @@
|
|||
*/
|
||||
package org.jivesoftware.smackx.ox.element;
|
||||
|
||||
import java.nio.charset.Charset;
|
||||
import java.util.Date;
|
||||
|
||||
import org.jivesoftware.smack.packet.ExtensionElement;
|
||||
import org.jivesoftware.smack.util.Objects;
|
||||
import org.jivesoftware.smack.util.XmlStringBuilder;
|
||||
import org.jivesoftware.smack.util.stringencoder.Base64;
|
||||
|
||||
/**
|
||||
* Class representing a pubkey element which is used to transport OpenPGP public keys.
|
||||
|
@ -88,21 +88,30 @@ public class PubkeyElement implements ExtensionElement {
|
|||
|
||||
public static final String ELEMENT = "data";
|
||||
|
||||
private final byte[] b64Data;
|
||||
private final String b64Data;
|
||||
|
||||
public PubkeyDataElement(byte[] b64Data) {
|
||||
public PubkeyDataElement(String b64Data) {
|
||||
this.b64Data = Objects.requireNonNull(b64Data);
|
||||
}
|
||||
|
||||
/**
|
||||
* Base64 encoded public key.
|
||||
*
|
||||
* @return public key bytes.
|
||||
* @return the base64 encoded version of the public key.
|
||||
*/
|
||||
public byte[] getB64Data() {
|
||||
public String getB64Data() {
|
||||
return b64Data;
|
||||
}
|
||||
|
||||
private transient byte[] pubKeyBytesCache;
|
||||
|
||||
public byte[] getPubKeyBytes() {
|
||||
if (pubKeyBytesCache == null) {
|
||||
pubKeyBytesCache = Base64.decode(b64Data);
|
||||
}
|
||||
return pubKeyBytesCache.clone();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getElementName() {
|
||||
return ELEMENT;
|
||||
|
@ -117,7 +126,7 @@ public class PubkeyElement implements ExtensionElement {
|
|||
public XmlStringBuilder toXML(org.jivesoftware.smack.packet.XmlEnvironment enclosingNamespace) {
|
||||
XmlStringBuilder xml = new XmlStringBuilder(this, enclosingNamespace)
|
||||
.rightAngleBracket()
|
||||
.append(new String(b64Data, Charset.forName("UTF-8")))
|
||||
.append(b64Data)
|
||||
.closeElement(this);
|
||||
return xml;
|
||||
}
|
||||
|
|
|
@ -17,7 +17,6 @@
|
|||
package org.jivesoftware.smackx.ox.provider;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.nio.charset.Charset;
|
||||
import java.util.Date;
|
||||
|
||||
import org.jivesoftware.smack.packet.XmlEnvironment;
|
||||
|
@ -34,7 +33,7 @@ import org.jivesoftware.smackx.ox.element.PubkeyElement;
|
|||
*/
|
||||
public class PubkeyElementProvider extends ExtensionElementProvider<PubkeyElement> {
|
||||
|
||||
public static final PubkeyElementProvider TEST_INSTANCE = new PubkeyElementProvider();
|
||||
public static final PubkeyElementProvider INSTANCE = new PubkeyElementProvider();
|
||||
|
||||
@Override
|
||||
public PubkeyElement parse(XmlPullParser parser, int initialDepth, XmlEnvironment xmlEnvironment) throws XmlPullParserException, IOException, SmackTextParseException {
|
||||
|
@ -46,11 +45,9 @@ public class PubkeyElementProvider extends ExtensionElementProvider<PubkeyElemen
|
|||
if (tag == XmlPullParser.Event.START_ELEMENT) {
|
||||
switch (name) {
|
||||
case PubkeyElement.PubkeyDataElement.ELEMENT:
|
||||
String data = parser.nextText();
|
||||
if (data != null) {
|
||||
byte[] bytes = data.getBytes(Charset.forName("UTF-8"));
|
||||
return new PubkeyElement(new PubkeyElement.PubkeyDataElement(bytes), date);
|
||||
}
|
||||
String base64EncodedOpenPgpPubKey = parser.nextText();
|
||||
PubkeyElement.PubkeyDataElement pubkeyDataElement = new PubkeyElement.PubkeyDataElement(base64EncodedOpenPgpPubKey);
|
||||
return new PubkeyElement(pubkeyDataElement, date);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -17,44 +17,48 @@
|
|||
package org.jivesoftware.smackx.ox;
|
||||
|
||||
import static junit.framework.TestCase.assertEquals;
|
||||
import static junit.framework.TestCase.assertTrue;
|
||||
import static org.jivesoftware.smack.test.util.XmlUnitUtils.assertXmlSimilar;
|
||||
|
||||
import java.nio.charset.Charset;
|
||||
import java.util.Arrays;
|
||||
import java.io.IOException;
|
||||
import java.text.ParseException;
|
||||
import java.util.Date;
|
||||
|
||||
import org.jivesoftware.smack.parsing.SmackParsingException;
|
||||
import org.jivesoftware.smack.test.util.SmackTestSuite;
|
||||
import org.jivesoftware.smack.test.util.TestUtils;
|
||||
import org.jivesoftware.smack.test.util.SmackTestUtil;
|
||||
import org.jivesoftware.smack.xml.XmlPullParser;
|
||||
import org.jivesoftware.smack.xml.XmlPullParserException;
|
||||
|
||||
import org.jivesoftware.smackx.ox.element.PubkeyElement;
|
||||
import org.jivesoftware.smackx.ox.provider.PubkeyElementProvider;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.EnumSource;
|
||||
import org.jxmpp.util.XmppDateTime;
|
||||
|
||||
public class PubkeyElementTest extends SmackTestSuite {
|
||||
|
||||
@Test
|
||||
public void providerTest() throws Exception {
|
||||
String expected =
|
||||
@ParameterizedTest
|
||||
@EnumSource(SmackTestUtil.XmlPullParserKind.class)
|
||||
public void providerTest(SmackTestUtil.XmlPullParserKind parserKind)
|
||||
throws ParseException, XmlPullParserException, IOException, SmackParsingException {
|
||||
String base64EncodedOpenPgpPublicKey = "VGhpcyBpcyBqdXN0IGEgdGVzdA==";
|
||||
String pubkeyElement =
|
||||
"<pubkey xmlns='urn:xmpp:openpgp:0' date='2018-01-21T10:46:21.000+00:00'>" +
|
||||
"<data>" +
|
||||
"BASE64_OPENPGP_PUBLIC_KEY" +
|
||||
base64EncodedOpenPgpPublicKey +
|
||||
"</data>" +
|
||||
"</pubkey>";
|
||||
|
||||
Date date = XmppDateTime.parseXEP0082Date("2018-01-21T10:46:21.000+00:00");
|
||||
byte[] key = "BASE64_OPENPGP_PUBLIC_KEY".getBytes(Charset.forName("UTF-8"));
|
||||
PubkeyElement element = new PubkeyElement(new PubkeyElement.PubkeyDataElement(key), date);
|
||||
PubkeyElement element = new PubkeyElement(new PubkeyElement.PubkeyDataElement(base64EncodedOpenPgpPublicKey), date);
|
||||
|
||||
assertXmlSimilar(expected, element.toXML().toString());
|
||||
assertXmlSimilar(pubkeyElement, element.toXML().toString());
|
||||
|
||||
XmlPullParser parser = TestUtils.getParser(expected);
|
||||
PubkeyElement parsed = PubkeyElementProvider.TEST_INSTANCE.parse(parser);
|
||||
XmlPullParser parser = SmackTestUtil.getParserFor(pubkeyElement, parserKind);
|
||||
PubkeyElement parsed = PubkeyElementProvider.INSTANCE.parse(parser);
|
||||
|
||||
assertEquals(element.getDate(), parsed.getDate());
|
||||
assertTrue(Arrays.equals(element.getDataElement().getB64Data(), parsed.getDataElement().getB64Data()));
|
||||
assertEquals(element.getDataElement().getB64Data(), parsed.getDataElement().getB64Data());
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue