mirror of
https://codeberg.org/Mercury-IM/Smack
synced 2025-12-09 22:51:07 +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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue