001/** 002 * 003 * Copyright 2017 Paul Schaub 004 * 005 * Licensed under the Apache License, Version 2.0 (the "License"); 006 * you may not use this file except in compliance with the License. 007 * You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017package org.jivesoftware.smackx.omemo.element; 018 019import org.jivesoftware.smack.packet.XmlElement; 020import org.jivesoftware.smack.packet.XmlEnvironment; 021import org.jivesoftware.smack.util.XmlStringBuilder; 022import org.jivesoftware.smack.util.stringencoder.Base64; 023 024import org.jivesoftware.smackx.omemo.util.OmemoConstants; 025 026/** 027 * Small class to collect key (byte[]), its id and whether its a preKey or not. 028 */ 029public class OmemoKeyElement implements XmlElement { 030 031 public static final String ELEMENT = "key"; 032 public static final String NAMESPACE = OmemoConstants.OMEMO_NAMESPACE_V_AXOLOTL; 033 034 public static final String ATTR_RID = "rid"; 035 public static final String ATTR_PREKEY = "prekey"; 036 037 private final byte[] data; 038 private final int id; 039 private final boolean preKey; 040 041 public OmemoKeyElement(byte[] data, int id) { 042 this(data, id, false); 043 } 044 045 public OmemoKeyElement(byte[] data, int id, boolean preKey) { 046 this.data = data; 047 this.id = id; 048 this.preKey = preKey; 049 } 050 051 public int getId() { 052 return this.id; 053 } 054 055 public byte[] getData() { 056 return this.data; 057 } 058 059 public boolean isPreKey() { 060 return this.preKey; 061 } 062 063 @Override 064 public String toString() { 065 return Integer.toString(id); 066 } 067 068 @Override 069 public String getElementName() { 070 return ELEMENT; 071 } 072 073 @Override 074 public String getNamespace() { 075 return NAMESPACE; 076 } 077 078 @Override 079 public XmlStringBuilder toXML(XmlEnvironment enclosingXmlEnvironment) { 080 XmlStringBuilder sb = new XmlStringBuilder(this, enclosingXmlEnvironment); 081 082 if (isPreKey()) { 083 sb.attribute(ATTR_PREKEY, true); 084 } 085 086 sb.attribute(ATTR_RID, getId()); 087 sb.rightAngleBracket(); 088 sb.append(Base64.encodeToString(getData())); 089 sb.closeElement(this); 090 return sb; 091 } 092}