Search in sources :

Example 1 with Feature

use of org.jivesoftware.smackx.packet.DiscoverInfo.Feature in project ecf by eclipse.

the class EntityCapsManager method generateVerificationString.

/**
 * Generates a XEP-115 Verification String
 *
 * @see <a href="http://xmpp.org/extensions/xep-0115.html#ver">XEP-115
 *      Verification String</a>
 *
 * @param discoverInfo
 * @param hash
 *            the used hash function
 * @return The generated verification String or null if the hash is not
 *         supported
 */
protected static String generateVerificationString(DiscoverInfo discoverInfo, String hash) {
    MessageDigest md = SUPPORTED_HASHES.get(hash.toLowerCase());
    if (md == null)
        return null;
    DataForm extendedInfo = (DataForm) discoverInfo.getExtension(Form.ELEMENT, Form.NAMESPACE);
    // 1. Initialize an empty string S ('sb' in this method).
    // Use StringBuilder as we don't
    StringBuilder sb = new StringBuilder();
    // need thread-safe StringBuffer
    // 2. Sort the service discovery identities by category and then by
    // type and then by xml:lang
    // (if it exists), formatted as CATEGORY '/' [TYPE] '/' [LANG] '/'
    // [NAME]. Note that each slash is included even if the LANG or
    // NAME is not included (in accordance with XEP-0030, the category and
    // type MUST be included.
    SortedSet<DiscoverInfo.Identity> sortedIdentities = new TreeSet<DiscoverInfo.Identity>();
    for (Iterator<DiscoverInfo.Identity> it = discoverInfo.getIdentities(); it.hasNext(); ) sortedIdentities.add(it.next());
    // followed by the '<' character.
    for (Iterator<DiscoverInfo.Identity> it = sortedIdentities.iterator(); it.hasNext(); ) {
        DiscoverInfo.Identity identity = it.next();
        sb.append(identity.getCategory());
        sb.append("/");
        sb.append(identity.getType());
        sb.append("/");
        sb.append(identity.getLanguage() == null ? "" : identity.getLanguage());
        sb.append("/");
        sb.append(identity.getName() == null ? "" : identity.getName());
        sb.append("<");
    }
    // 4. Sort the supported service discovery features.
    SortedSet<String> features = new TreeSet<String>();
    for (Iterator<Feature> it = discoverInfo.getFeatures(); it.hasNext(); ) features.add(it.next().getVar());
    // character
    for (String f : features) {
        sb.append(f);
        sb.append("<");
    }
    // see XEP-0115 5.4 step 3.6
    if (extendedInfo != null && extendedInfo.hasHiddenFormTypeField()) {
        synchronized (extendedInfo) {
            // 6. If the service discovery information response includes
            // XEP-0128 data forms, sort the forms by the FORM_TYPE (i.e.,
            // by the XML character data of the <value/> element).
            SortedSet<FormField> fs = new TreeSet<FormField>(new Comparator<FormField>() {

                public int compare(FormField f1, FormField f2) {
                    return f1.getVariable().compareTo(f2.getVariable());
                }
            });
            FormField ft = null;
            for (Iterator<FormField> i = extendedInfo.getFields(); i.hasNext(); ) {
                FormField f = i.next();
                if (!f.getVariable().equals("FORM_TYPE")) {
                    fs.add(f);
                } else {
                    ft = f;
                }
            }
            // Add FORM_TYPE values
            if (ft != null) {
                formFieldValuesToCaps(ft.getValues(), sb);
            }
            // followed by the '<' character.
            for (FormField f : fs) {
                sb.append(f.getVariable());
                sb.append("<");
                formFieldValuesToCaps(f.getValues(), sb);
            }
        }
    }
    // 8. Ensure that S is encoded according to the UTF-8 encoding (RFC
    // 3269).
    // 9. Compute the verification string by hashing S using the algorithm
    // specified in the 'hash' attribute (e.g., SHA-1 as defined in RFC
    // 3174).
    // The hashed data MUST be generated with binary output and
    // encoded using Base64 as specified in Section 4 of RFC 4648
    // (note: the Base64 output MUST NOT include whitespace and MUST set
    // padding bits to zero).
    byte[] digest = md.digest(sb.toString().getBytes());
    return Base64.encodeBytes(digest);
}
Also used : DiscoverInfo(org.jivesoftware.smackx.packet.DiscoverInfo) Feature(org.jivesoftware.smackx.packet.DiscoverInfo.Feature) Identity(org.jivesoftware.smackx.packet.DiscoverInfo.Identity) TreeSet(java.util.TreeSet) DataForm(org.jivesoftware.smackx.packet.DataForm) MessageDigest(java.security.MessageDigest) Identity(org.jivesoftware.smackx.packet.DiscoverInfo.Identity) FormField(org.jivesoftware.smackx.FormField)

Aggregations

MessageDigest (java.security.MessageDigest)1 TreeSet (java.util.TreeSet)1 FormField (org.jivesoftware.smackx.FormField)1 DataForm (org.jivesoftware.smackx.packet.DataForm)1 DiscoverInfo (org.jivesoftware.smackx.packet.DiscoverInfo)1 Feature (org.jivesoftware.smackx.packet.DiscoverInfo.Feature)1 Identity (org.jivesoftware.smackx.packet.DiscoverInfo.Identity)1