Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Instant Stream Resumption implementation #92

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions documentation/extensions/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ Currently supported XEPs of smack-tcp
| Name | XEP | Description |
|---------------------------------------------|----------------------------------------------------------|----------------------------------------------------------------------------------------------------------|
| [Stream Management](streammanagement.md) | [XEP-0198](http://xmpp.org/extensions/xep-0198.html) | Allows active management of an XML Stream between two XMPP entities (stanza acknowledgement, stream resumption). |
| Instant Stream Resumption | [XEP-xxxx](http://xmpp.org/extensions/inbox/isr.html) | Allows XMPP entities to instantaneously resume an XMPP stream.. |


Smack Extensions and currently supported XEPs of smack-extensions
Expand Down
75 changes: 75 additions & 0 deletions smack-tcp/src/main/java/org/jivesoftware/smack/isr/HMAC.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/**
*
* Copyright © 2016 Fernando Ramirez
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jivesoftware.smack.isr;

import java.io.UnsupportedEncodingException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;

import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;

/**
* Instant Stream Resumption HMAC class.
*
* @author Fernando Ramirez
* @see <a href="http://xmpp.org/extensions/inbox/isr.html">XEP-xxxx: Instant
* Stream Resumption</a>
*
*/
public class HMAC {

/**
* Get the HMAC digest.
*
* @param msg
* @param keyString
* @param algo
* @return the HMAC digest
*/
public static String hmacDigest(String msg, String keyString, String algo) {
String digest = null;
try {
SecretKeySpec key = new SecretKeySpec((keyString).getBytes("UTF-8"), algo);

String algorithm;
if (!algo.toLowerCase().startsWith("hmac")) {
algorithm = "hmac" + algo.toLowerCase();
} else {
algorithm = algo.toLowerCase();
}

Mac mac = Mac.getInstance(algorithm);
mac.init(key);

byte[] bytes = mac.doFinal(msg.getBytes("ASCII"));

StringBuffer hash = new StringBuffer();
for (int i = 0; i < bytes.length; i++) {
String hex = Integer.toHexString(0xFF & bytes[i]);
if (hex.length() == 1) {
hash.append('0');
}
hash.append(hex);
}
digest = hash.toString();
} catch (UnsupportedEncodingException | InvalidKeyException | NoSuchAlgorithmException e) {
throw new IllegalStateException("HMAC digest could not be obtained correctly.", e);
}
return digest;
}
}
49 changes: 49 additions & 0 deletions smack-tcp/src/main/java/org/jivesoftware/smack/isr/ISRUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/**
*
* Copyright © 2016 Fernando Ramirez
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jivesoftware.smack.isr;

import java.io.IOException;

import org.jivesoftware.smack.isr.element.InstantStreamResumption;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;

/**
* Instant Stream Resumption utils.
*
* @author Fernando Ramirez
* @see <a href="http://xmpp.org/extensions/inbox/isr.html">XEP-xxxx: Instant
* Stream Resumption</a>
*
*/
public class ISRUtils {

/**
* Check if is a nonza from Instant Stream Resumption.
*
* @param parser
* @return true if is a nonza from Instant Stream Resumption
* @throws XmlPullParserException
* @throws IOException
*/
public static boolean isISRNonza(XmlPullParser parser) throws XmlPullParserException, IOException {
String isrNamespace = parser.getNamespace(InstantStreamResumption.NAMESPACE_PREFIX);
return (isrNamespace != null && isrNamespace.equals(InstantStreamResumption.NAMESPACE))
|| parser.getNamespace().equals(InstantStreamResumption.NAMESPACE);
}

}
Loading