Javaトークンジェネレータ

6277 ワード

 1 package Token;

 2 import java.security.MessageDigest;

 3 import java.security.NoSuchAlgorithmException;

 4 import java.text.SimpleDateFormat;

 5 import java.util.Date;

 6 import java.util.Random;

 7 import Decoder.BASE64Encoder;

 8 

 9 //     

10 public class TokenProcessor {

11     private TokenProcessor(){}

12     private static TokenProcessor instance = new TokenProcessor();

13     public static TokenProcessor getInstance(){

14         return instance;

15     }

16     public String generateTokeCode(){

17         String value = System.currentTimeMillis()+new Random().nextInt()+"";

18         System.out.println(value); 

19         

20         

21         long currentTime = System.currentTimeMillis();

22         SimpleDateFormat formatter = new SimpleDateFormat("yyyy -MM dd -HH mm ss ");

23         Date date = new Date(currentTime);

24         System.out.println(formatter.format(date));

25         

26 

27         //

28         try {

29             MessageDigest md = MessageDigest.getInstance("md5");

30             byte[] b = md.digest(value.getBytes());//       

31             //Base64  

32             BASE64Encoder be = new BASE64Encoder();

33             be.encode(b);

34             System.out.println(be.encode(b)); 

35             return be.encode(b);//      

36         } catch (NoSuchAlgorithmException e){

37             e.printStackTrace();

38         }

39         return null;

40     }

41     public static void main(String[] args) {

42         TokenProcessor processor=new TokenProcessor();

43         processor.generateTokeCode();

44     }

45 }