publicstaticintgenerateCode(byte[] key,long t)throws NoSuchAlgorithmException, InvalidKeyException {byte[] data =newbyte[8];long value = t;for(int i =8; i-->0; value >>>=8){
data[i]=(byte) value;}
SecretKeySpec signKey =newSecretKeySpec(key,"HmacSHA1");
Mac mac = Mac.getInstance("HmacSHA1");
mac.init(signKey);byte[] hash = mac.doFinal(data);int offset = hash[20-1]&0xF;// We're using a long because Java hasn't got unsigned int.long truncatedHash =0;for(int i =0; i <4;++i){
truncatedHash <<=8;// We are dealing with signed bytes:// we just keep the first byte.
truncatedHash |=(hash[offset + i]&0xFF);}
truncatedHash &=0x7FFFFFFF;
truncatedHash %=1000000;return(int) truncatedHash;}