abdera-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jmsn...@apache.org
Subject svn commit: r1175285 - in /abdera/abdera2/common/src/main/java/org/apache/abdera2/common/security: ./ ApiKey.java KeyBase.java Otp.java
Date Sat, 24 Sep 2011 23:55:17 GMT
Author: jmsnell
Date: Sat Sep 24 23:55:17 2011
New Revision: 1175285

URL: http://svn.apache.org/viewvc?rev=1175285&view=rev
Log:
Miscellaneous security extras.. utility classes for generating a random api key based on a
cryptographic hash, and an implementation of the HOTP/TOTP one-time password algorithm. These
are basically there as a useful utility for application apis

Added:
    abdera/abdera2/common/src/main/java/org/apache/abdera2/common/security/
    abdera/abdera2/common/src/main/java/org/apache/abdera2/common/security/ApiKey.java   (with
props)
    abdera/abdera2/common/src/main/java/org/apache/abdera2/common/security/KeyBase.java  
(with props)
    abdera/abdera2/common/src/main/java/org/apache/abdera2/common/security/Otp.java   (with
props)

Added: abdera/abdera2/common/src/main/java/org/apache/abdera2/common/security/ApiKey.java
URL: http://svn.apache.org/viewvc/abdera/abdera2/common/src/main/java/org/apache/abdera2/common/security/ApiKey.java?rev=1175285&view=auto
==============================================================================
--- abdera/abdera2/common/src/main/java/org/apache/abdera2/common/security/ApiKey.java (added)
+++ abdera/abdera2/common/src/main/java/org/apache/abdera2/common/security/ApiKey.java Sat
Sep 24 23:55:17 2011
@@ -0,0 +1,96 @@
+package org.apache.abdera2.common.security;
+
+import javax.crypto.spec.SecretKeySpec;
+
+import org.apache.commons.codec.binary.Base64;
+import org.apache.commons.codec.binary.Hex;
+
+/**
+ * Utility Class used for Generating API Keys
+ */
+public class ApiKey extends KeyBase {
+
+  public ApiKey(byte[] key, int size) {
+    super(key, size);
+  }
+
+  public ApiKey(byte[] key, String alg, int size) {
+    super(key, alg, size);
+  }
+
+  public ApiKey(SecretKeySpec key, int size) {
+    super(key, size);
+  }
+
+  public ApiKey(SecretKeySpec key, String alg, int size) {
+    super(key, alg, size);
+  }
+
+  public ApiKey(SecretKeySpec key) {
+    super(key);
+  }
+
+  public ApiKey(String key, int size) {
+    super(key, size);
+  }
+
+  public ApiKey(String key, String alg, int size) {
+    super(key, alg, size);
+  }
+
+  public ApiKey(String key) {
+    super(key);
+  }
+
+  public String generateNext() {
+    int len = Math.min(20, size);
+    byte[] buf = hmac(randomBytes(len));
+    buf = Base64.encodeBase64(buf, false, true);
+    StringBuilder sb = new StringBuilder();
+    for (byte b : buf)
+      sb.append(Character.isLetterOrDigit(b)?(char)b:'.');
+    return sb.toString();
+  }
+ 
+  public String generateNextHex() {
+    int len = Math.min(20, size);
+    byte[] buf = hmac(randomBytes(len));
+    return Hex.encodeHexString(buf);
+  }
+  
+  public static ApiKey WEAK(byte[] key) {
+    return new ApiKey(key,"HmacSHA1",20);
+  }
+  
+  public static ApiKey WEAK(SecretKeySpec key) {
+    return new ApiKey(key,"HmacSHA1",20);
+  }
+  
+  public static ApiKey WEAK(String key) {
+    return new ApiKey(key,"HmacSHA1",20);
+  }
+  
+  public static ApiKey MEDIUM(byte[] key) {
+    return new ApiKey(key,"HmacSHA256",256);
+  }
+  
+  public static ApiKey MEDIUM(SecretKeySpec key) {
+    return new ApiKey(key,"HmacSHA256",256);
+  }
+  
+  public static ApiKey MEDIUM(String key) {
+    return new ApiKey(key,"HmacSHA256",256);
+  }
+  
+  public static ApiKey STRONG(byte[] key) {
+    return new ApiKey(key,"HmacSHA512",512);
+  }
+  
+  public static ApiKey STRONG(SecretKeySpec key) {
+    return new ApiKey(key,"HmacSHA512",512);
+  }
+  
+  public static ApiKey STRONG(String key) {
+    return new ApiKey(key,"HmacSHA512",512);
+  }
+}

Propchange: abdera/abdera2/common/src/main/java/org/apache/abdera2/common/security/ApiKey.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: abdera/abdera2/common/src/main/java/org/apache/abdera2/common/security/KeyBase.java
URL: http://svn.apache.org/viewvc/abdera/abdera2/common/src/main/java/org/apache/abdera2/common/security/KeyBase.java?rev=1175285&view=auto
==============================================================================
--- abdera/abdera2/common/src/main/java/org/apache/abdera2/common/security/KeyBase.java (added)
+++ abdera/abdera2/common/src/main/java/org/apache/abdera2/common/security/KeyBase.java Sat
Sep 24 23:55:17 2011
@@ -0,0 +1,84 @@
+package org.apache.abdera2.common.security;
+import java.security.SecureRandom;
+
+import javax.crypto.Mac;
+import javax.crypto.spec.SecretKeySpec;
+
+import org.apache.commons.codec.binary.Hex;
+
+public abstract class KeyBase {
+
+  public static final String DEFAULT_ALG="HmacSHA256";
+  public static final int DEFAULT_SIZE=256;
+  
+  protected final SecretKeySpec key;
+  protected final String alg;
+  protected final int size;
+  
+  public abstract String generateNext();
+  
+  public KeyBase(SecretKeySpec key) {
+    this(key,DEFAULT_ALG,DEFAULT_SIZE);
+  }
+  
+  public KeyBase(SecretKeySpec key, int size) {
+    this(key,DEFAULT_ALG,size);
+  }
+  
+  public KeyBase(SecretKeySpec key, String alg, int size) { 
+    this.key = key;
+    this.alg = alg;
+    this.size = size;
+  }
+  
+  public KeyBase(String key) {
+    this(key,DEFAULT_ALG,DEFAULT_SIZE);
+  }
+  
+  public KeyBase(String key, int size) {
+    this(key,DEFAULT_ALG,size);
+  }
+  
+  public KeyBase(byte[] key, String alg, int size) {
+    this.key = secret(key);
+    this.alg = alg;
+    this.size = size;
+  }
+  
+  public KeyBase(byte[] key, int size) {
+    this(key,DEFAULT_ALG,size);
+  }
+  
+  public KeyBase(String key, String alg, int size) {
+    this(dec(key),alg,size);
+  }
+  
+  protected SecretKeySpec secret(byte[] key) {
+    return new SecretKeySpec(key, "RAW");
+  }
+  
+  protected static byte[] dec(String val) {
+    try {
+      return Hex.decodeHex(val.toCharArray());
+    } catch (Throwable t) {
+      throw new RuntimeException(t);
+    }
+  }
+  
+  protected byte[] hmac(byte[] mat){
+    try {
+      Mac hmac = Mac.getInstance(alg);
+      hmac.init(key);
+      return hmac.doFinal(mat);
+    } catch (Throwable t) {
+      throw new RuntimeException(t);
+    }
+}
+  
+  protected byte[] randomBytes(int count) {
+    SecureRandom random = new SecureRandom();
+    byte[] buf = new byte[count];
+    random.nextBytes(buf);
+    return buf;
+  }
+}

Propchange: abdera/abdera2/common/src/main/java/org/apache/abdera2/common/security/KeyBase.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: abdera/abdera2/common/src/main/java/org/apache/abdera2/common/security/Otp.java
URL: http://svn.apache.org/viewvc/abdera/abdera2/common/src/main/java/org/apache/abdera2/common/security/Otp.java?rev=1175285&view=auto
==============================================================================
--- abdera/abdera2/common/src/main/java/org/apache/abdera2/common/security/Otp.java (added)
+++ abdera/abdera2/common/src/main/java/org/apache/abdera2/common/security/Otp.java Sat Sep
24 23:55:17 2011
@@ -0,0 +1,128 @@
+package org.apache.abdera2.common.security;
+import javax.crypto.spec.SecretKeySpec;
+
+
+/**
+ * Utility class for generating One-Time-Passwords using the HOTP algorithm
+ */
+public abstract class Otp extends KeyBase {
+
+  protected Otp(byte[] key, int size) {
+    super(key, size);
+  }
+
+  protected Otp(byte[] key) {
+    super(key,8);
+  }
+  
+  protected Otp(byte[] key, String alg, int size) {
+    super(key, alg, size);
+  }
+
+  protected Otp(SecretKeySpec key, int size) {
+    super(key, size);
+  }
+
+  protected Otp(SecretKeySpec key, String alg, int size) {
+    super(key, alg, size);
+  }
+
+  protected Otp(SecretKeySpec key) {
+    super(key,8);
+  }
+
+  protected Otp(String key, int size) {
+    super(key, size);
+  }
+
+  protected Otp(String key, String alg, int size) {
+    super(key, alg, size);
+  }
+
+  protected Otp(String key) {
+    super(key,8);
+  }
+
+  protected abstract String getMaterial();
+  
+  public String generateNext(){
+    String mat = getMaterial();
+    int len = Math.max(1, Math.min(9, size));
+    while (mat.length() < 16 )
+      mat = "0" + mat;
+    byte[] h = hmac(dec(mat));
+    int o = h[h.length - 1] & 0xf;
+    int binary =
+        ((h[o] & 0x7f) << 24) |
+        ((h[o + 1] & 0xff) << 16) |
+        ((h[o + 2] & 0xff) << 8) |
+        (h[o + 3] & 0xff);
+    int otp = binary % (int)Math.pow(10, len);
+    String r = Integer.toString(otp);
+    while (r.length() < len)
+        r = "0" + r;
+    return r;
+  }
+  
+  /**
+   * Utility implementation of the Time-based One Time Password (TOTP) 
+   * algorithm. 
+   */
+  public static final class Totp extends Otp {
+
+    private final int step;
+    
+    public Totp(int step, byte[] key, int size) {
+      super(key, size);
+      this.step = step;
+    }
+
+    public Totp(int step, byte[] key) {
+      super(key,8);
+      this.step = step;
+    }
+    
+    public Totp(int step, byte[] key, String alg, int size) {
+      super(key, alg, size);
+      this.step = step;
+    }
+
+    public Totp(int step, SecretKeySpec key, int size) {
+      super(key, size);
+      this.step = step;
+    }
+
+    public Totp(int step, SecretKeySpec key, String alg, int size) {
+      super(key, alg, size);
+      this.step = step;
+    }
+
+    public Totp(int step, SecretKeySpec key) {
+      super(key);
+      this.step = step;
+    }
+
+    public Totp(int step, String key, int size) {
+      super(key, size);
+      this.step = step;
+    }
+
+    public Totp(int step, String key, String alg, int size) {
+      super(key, alg, size);
+      this.step = step;
+    }
+
+    public Totp(int step, String key) {
+      super(key);
+      this.step = step;
+    }
+
+    @Override
+    protected String getMaterial() {
+      long t = (System.currentTimeMillis() / 1000l) / step;
+      String r = Long.toHexString(t);
+      while(r.length()<16) r = "0"+r;
+      return r;
+    }
+  }
+}

Propchange: abdera/abdera2/common/src/main/java/org/apache/abdera2/common/security/Otp.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain



Mime
View raw message