OmemoKeyElement.java

  1. /**
  2.  *
  3.  * Copyright 2017 Paul Schaub
  4.  *
  5.  * Licensed under the Apache License, Version 2.0 (the "License");
  6.  * you may not use this file except in compliance with the License.
  7.  * You may obtain a copy of the License at
  8.  *
  9.  *     http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */
  17. package org.jivesoftware.smackx.omemo.element;

  18. import org.jivesoftware.smack.packet.XmlElement;
  19. import org.jivesoftware.smack.packet.XmlEnvironment;
  20. import org.jivesoftware.smack.util.XmlStringBuilder;
  21. import org.jivesoftware.smack.util.stringencoder.Base64;

  22. import org.jivesoftware.smackx.omemo.util.OmemoConstants;

  23. /**
  24.  * Small class to collect key (byte[]), its id and whether its a preKey or not.
  25.  */
  26. public class OmemoKeyElement implements XmlElement {

  27.     public static final String ELEMENT = "key";
  28.     public static final String NAMESPACE = OmemoConstants.OMEMO_NAMESPACE_V_AXOLOTL;

  29.     public static final String ATTR_RID = "rid";
  30.     public static final String ATTR_PREKEY = "prekey";

  31.     private final byte[] data;
  32.     private final int id;
  33.     private final boolean preKey;

  34.     public OmemoKeyElement(byte[] data, int id) {
  35.         this(data, id, false);
  36.     }

  37.     public OmemoKeyElement(byte[] data, int id, boolean preKey) {
  38.         this.data = data;
  39.         this.id = id;
  40.         this.preKey = preKey;
  41.     }

  42.     public int getId() {
  43.         return this.id;
  44.     }

  45.     public byte[] getData() {
  46.         return this.data;
  47.     }

  48.     public boolean isPreKey() {
  49.         return this.preKey;
  50.     }

  51.     @Override
  52.     public String toString() {
  53.         return Integer.toString(id);
  54.     }

  55.     @Override
  56.     public String getElementName() {
  57.         return ELEMENT;
  58.     }

  59.     @Override
  60.     public String getNamespace() {
  61.         return NAMESPACE;
  62.     }

  63.     @Override
  64.     public XmlStringBuilder toXML(XmlEnvironment enclosingXmlEnvironment) {
  65.         XmlStringBuilder sb = new XmlStringBuilder(this, enclosingXmlEnvironment);

  66.         if (isPreKey()) {
  67.             sb.attribute(ATTR_PREKEY, true);
  68.         }

  69.         sb.attribute(ATTR_RID, getId());
  70.         sb.rightAngleBracket();
  71.         sb.append(Base64.encodeToString(getData()));
  72.         sb.closeElement(this);
  73.         return sb;
  74.     }
  75. }