2011-06-12 15 views
6

Escribí un generador de contraseña única (OTP) en C# el año pasado. Ahora necesito usar un generador OTP en Java pero no pude encontrar las funciones equivalentes en Java.Contraseña de un solo uso (OTP) C# a la conversión de Java del código

Este es el código que escribí el año pasado: (Sé seguridad de este OTP es bajo, pero no necesito una prueba de balas)

SHA1CryptoServiceProvider hash = new SHA1CryptoServiceProvider(); //first hash with sha1 
byte[] hashPass = hash.ComputeHash(Encoding.ASCII.GetBytes(pass)); //pass is entered by user 
HMACMD5 hma = new HMACMD5(hashPass); // use the hashed value as a key to hmac 
OTPass = hma.ComputeHash(Encoding.ASCII.GetBytes(email + Counter(email)));// generate OTPass, Counter(email) is the counter of the user taken from database 
increaseCounter(email); // updating the counter 
this.SetLog(this.GetLog() + Environment.NewLine + "OTPass Generated: " + BitConverter.ToString(OTPass)); // OTP 

Aquí está el código de Java He intentado convertir C# en: (Esto es sólo la parte SHA1, no pude encontrar la forma de escribir HMAC-MD5 en Java)

import java.io.*; 
import java.security.*; 

public class otp { 

/** 
* @param args 
* @throws IOException 
*/ 
public static void main(String[] args) throws IOException { 

    System.out.println("Please enter your username:"); 
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 
    String username = br.readLine(); 
    System.out.println("Please enter your password:"); 
    String password = br.readLine(); 

    try { 
     MessageDigest md = MessageDigest.getInstance("SHA1"); 

     String input = password; 
     md.update(input.getBytes()); 
     byte[] output = md.digest(); 
     System.out.println(); 
     System.out.println("SHA1(\""+input+"\") ="); 
     System.out.println(" "+bytesToHex(output)); 


     } catch (Exception e) { 
     System.out.println("Exception: "+e); 
     } 
    } 
    public static String bytesToHex(byte[] b) { 
     char hexDigit[] = {'0', '1', '2', '3', '4', '5', '6', '7', 
         '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'}; 
     StringBuffer buf = new StringBuffer(); 
     for (int j=0; j<b.length; j++) { 
     buf.append(hexDigit[(b[j] >> 4) & 0x0f]); 
     buf.append(hexDigit[b[j] & 0x0f]); 
     } 
     return buf.toString();   
} 
} 

Gracias por la ayuda

+3

Para aquellos que no lo saben, OTP es un acrónimo de "contraseña de un solo uso". (Estoy tratando de ahorrar ancho de banda para Wikipedia :-).) –

Respuesta

Cuestiones relacionadas