Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  // Yubico Java client class that calls Yubico authentication server to
  // validate an OTP (One-Time Password) generated by a Yubikey.
  // For specification see <>.
  // Copyright (c) 2008, Yubico AB.  All rights reserved.
  // Redistribution and use in source and binary forms, with or without
 // modification, are permitted provided that the following conditions
 // are met:
 // * Redistributions of source code must retain the above copyright
 //   notice, this list of conditions and the following disclaimer.
 // * Redistributions in binary form must reproduce the above copyright
 //   notice, this list of conditions and the following
 //   disclaimer in the documentation and/or other materials provided
 //   with the distribution.
 // Written by Paul Chen <>, March 2008.
 package com.yubico.client.v1;
Your app instantiate an object of this class, then call verify(OTP) to validate the one-time password (OTP) generated by Yubikey
 public class YubicoClient {
 	static final String YUBICO_AUTH_SRV_URL = "";
 	private int _clientId;
 	private String _response;

Initializes the Yubico object.

initId The Client ID you wish to verify against or operate within.
 	public YubicoClientint initId ) {
 		 = initId;

Returns the ID passed to the initialized Yubico object.

id The Client ID passed to the initializing class.
 	public int getId() {
 		return ;
 	public String getLastResponse() {
 		return ;

Verify otp against online verification service.

otp The OTP to verify, in modhex format.
bool Verified OK or not?
 	public boolean verifyString otp ) {
 		boolean result = false;
 		try {			
 	        URL srv = new URL( +  + "&otp=" + otp);
 	        URLConnection conn = srv.openConnection(); 
 	        BufferedReader in = new BufferedReader(new InputStreamReader(
 	        String inputLine;
 	        while ((inputLine = in.readLine()) != null) { 
 		    if (inputLine.startsWith("status=")) {
 	            	if (inputLine.equals("status=OK")) {
 	            		result = true;
catch (Exception e) {
			..println("Error! " + e.getMessage());
		return result;
// End of verify
Extract the public ID of a Yubikey from an OTP it generated.

otp The OTP to extract ID from, in modhex format.
string Public ID of Yubikey that generated otp. Between 0 and 12 characters.
	public String getPublicId(String otp) {
		Integer len = otp.length();
		/* The OTP part is always the last 32 bytes of otp. Whatever is before that
		 * (if anything) is the public ID of the Yubikey. The ID can be set to ''
		 * through personalization.
		return otp.substring(0, len - 32);
// End of getPublicId
	public static void main (String args []) throws Exception
		if (args.length != 2) {
            ..println("\n*** Test your Yubikey against Yubico OTP validation server ***");
            ..println("\nUsage: java com.yubico.client.v1.YubicoClient Auth_ID OTP");
            ..println("\nEg. java com.yubico.client.v1.YubicoClient 28 vvfucnlcrrnejlbuthlktguhclhvegbungldcrefbnku");
            ..println("\nTouch Yubikey to generate the OTP. Visit for more details.");
		int authId = Integer.parseInt(args[0]);
		String otp = args[1];
		YubicoClient yc = new YubicoClient(authId);
		if (yc.verify(otp)) {
			..println("\n* OTP verified OK");
else {
			..println("\n* Failed to verify OTP");
		..println("\n* Last response:\n" + yc.getLastResponse());
// End of main
// End of class
New to GrepCode? Check out our FAQ X