Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
60 changes: 60 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,60 @@
/**
*
* 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 {

public static String hmacDigest(String msg, String keyString, String algo) {
String digest = null;
try {
SecretKeySpec key = new SecretKeySpec((keyString).getBytes("UTF-8"), algo);
Mac mac = Mac.getInstance(algo);
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 e) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Never swallow Exceptions! Use a try/multi-catch and throw a unchecked exception, e.g. InvalidStateException in this case. In cases where the exception can be ignored, log it at least with a FINE/FINER/FINEST level.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Flowdalic fixed

} catch (InvalidKeyException e) {
} catch (NoSuchAlgorithmException 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