Android Open Source - amanaje Totp Impl






From Project

Back to project page amanaje.

License

The source code is released under:

Apache License

If you think the Android project amanaje listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.amanaje.crypto;
//from   w ww  .  j a  v a  2  s. c  o m
import java.lang.reflect.UndeclaredThrowableException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;

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

import com.amanaje.commons.Utils;

/**
 * This is a minimal implementation of the OATH TOTP algorithm.
 * This code is based on RFC6238 (http://tools.ietf.org/html/rfc6238)
 * and it is a derivation from a previous code written by Johan Rydell
 *
 * @author Jose Damico <jd.comment@gmail.com>
 */
public class TotpImpl {
                  //  0 1  2   3    4     5      6       7        8
  private final int[] DIGITS_POWER = {1,10,100,1000,10000,100000,1000000,10000000,100000000 };
  private static TotpImpl INSTANCE = null;
  public static TotpImpl getInstance(){
    if(INSTANCE == null) INSTANCE = new TotpImpl();
    return INSTANCE;
  }

  private TotpImpl() {}

  /**
   * This method uses the JCE to provide the crypto algorithm.
   * HMAC computes a Hashed Message Authentication Code with the
   * crypto hash algorithm as a parameter.
   * @param crypto     the crypto algorithm (HmacSHA1, HmacSHA256, HmacSHA512)
   * @param keyBytes   the bytes to use for the HMAC key
   * @param text       the message or text to be authenticated.
   * @throws UndeclaredThrowableException
   * @throws NoSuchAlgorithmException 
   * @throws InvalidKeyException 
   */
  private byte[] hmac_sha1(String crypto, byte[] keyBytes, byte[] text) throws UndeclaredThrowableException, NoSuchAlgorithmException, InvalidKeyException {
    Mac hmac;
    hmac = Mac.getInstance(crypto);
    SecretKeySpec macKey = new SecretKeySpec(keyBytes, "RAW");
    hmac.init(macKey);
    return hmac.doFinal(text);
  }



  
  /**
   * This method generates an TOTP value for the given set of parameters.public class TotpImpl {

}
s
   * @param key   the shared secret
   * @param time     a value that reflects a time
   * @param returnDigits     number of digits to return
   * @return      A numeric String in base 10 that includes truncationDigits digits
   * @throws NoSuchAlgorithmException 
   * @throws UndeclaredThrowableException 
   * @throws InvalidKeyException 
   */
  public String generateTOTP(byte[] key, String time,  int returnDigits) throws InvalidKeyException, UndeclaredThrowableException, NoSuchAlgorithmException {
    return generateTOTP(key, time, returnDigits, "HmacSHA1");
  }

  public String generateTOTP256(byte[] key, String time,  int returnDigits) throws InvalidKeyException, UndeclaredThrowableException, NoSuchAlgorithmException {
    return generateTOTP(key, time, returnDigits, "HmacSHA256");
  }

  public String generateTOTP512(byte[] key, String time,  int returnDigits) throws InvalidKeyException, UndeclaredThrowableException, NoSuchAlgorithmException {
    return generateTOTP(key, time, returnDigits, "HmacSHA512");
  }
  
  public String generateTOTP(byte[] key, String time, int codeDigits, String crypto) throws InvalidKeyException, UndeclaredThrowableException, NoSuchAlgorithmException {
    
    // Using the counter
    // First 8 bytes are for the movingFactor
    // Complaint with base RFC 4226 (HOTP)

    while(time.length() < 16 ) time = "0" + time;
    byte[] msg = Utils.getInstance().hexStringToByteArray(time); // Get the HEX in a byte[]

    // Adding one byte to get the right conversion

    byte[] hash = hmac_sha1(crypto, key, msg);
    int offset = hash[hash.length - 1] & 0xf; // put selected bytes into result int
    int binary =
        ((hash[offset] & 0x7f) << 24) |
        ((hash[offset + 1] & 0xff) << 16) |
        ((hash[offset + 2] & 0xff) << 8) |
        (hash[offset + 3] & 0xff);
    int otp = binary % DIGITS_POWER[codeDigits];
    String result = Integer.toString(otp);
    while (result.length() < codeDigits) result = "0" + result;
    return result;
  }
}




Java Source Code List

com.amanaje.activities.ContactDetailActivity.java
com.amanaje.activities.MainActivity.java
com.amanaje.activities.MessageActivity.java
com.amanaje.activities.NewSmsActivity.java
com.amanaje.activities.PrivContactsActivity.java
com.amanaje.activities.SettingsActivity.java
com.amanaje.activities.package-info.java
com.amanaje.asynctasks.AsyncTaskManager.java
com.amanaje.asynctasks.package-info.java
com.amanaje.commons.ActivityHelper.java
com.amanaje.commons.AppException.java
com.amanaje.commons.AppMessages.java
com.amanaje.commons.Constants.java
com.amanaje.commons.StaticObj.java
com.amanaje.commons.Utils.java
com.amanaje.commons.package-info.java
com.amanaje.crypto.CryptoUtils.java
com.amanaje.crypto.TotpImpl.java
com.amanaje.crypto.package-info.java
com.amanaje.entities.ConfigEntity.java
com.amanaje.entities.CryptoAlgoEntity.java
com.amanaje.entities.OpenPgpEntity.java
com.amanaje.entities.SmsEntity.java
com.amanaje.entities.package-info.java
com.amanaje.view.adapters.RowContactAdapter.java
com.amanaje.view.adapters.StableArrayAdapter.java
com.amanaje.view.adapters.package-info.java
org.jdamico.bc.openpgp.utils.PgpHelper.java
org.jdamico.bc.openpgp.utils.RSAKeyPairGenerator.java
org.jdamico.bc.openpgp.utils.package-info.java