UUID与获取随机验证码

UUID是Universally Unique Identifier的缩写,它是在一定的范围内(从特定的名字空间到全球)唯一的机器生成的标识符。

    public static void main(String[] args) {
        //获得六位验证码(包括小写字母和数字,不包括大写字母)
//        String result = UUID.randomUUID().toString().replaceAll("-", "").substring(0, 6);
        UUID uuid = UUID.randomUUID();
        String string1 = uuid.toString();
        String string2 = string1.replaceAll("-", "");
        String string3 = string2.substring(0, 6);
        System.out.println("验证码为:" + uuid);//77be56a9-b516-4739-8f04-61a11a2e25d4
        System.out.println("验证码为:" + string1);//77be56a9-b516-4739-8f04-61a11a2e25d4
        System.out.println("验证码为:" + string2);//77be56a9b51647398f0461a11a2e25d4
        System.out.println("验证码为:" + string3);//77be56
    }

不使用UUID获取随机:

  public static void main(String[] args) {
        // 取随机产生的认证码(6位数字)
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < 6; i++) {
            String str = getRandomChar();
            sb.append(str);
        }
        System.out.print("验证码为:" + sb.toString());
    }


    public static String getRandomChar() {
        int index = (int) Math.round(Math.random() * 2);//随机选择大小写字母还是数字[0,2)
        String randChar = "";
        switch (index) {
            case 0:// 大写字符
                randChar = String.valueOf((char) Math.round(Math.random() * 25 + 65));
                break;
            case 1:// 小写字符
                randChar = String.valueOf((char) Math.round(Math.random() * 25 + 97));
                break;
            default:// 数字
                randChar = String.valueOf(Math.round(Math.random() * 9));
                break;
        }
        return randChar;
    }

综上可得:

  public static void main(String[] args) {
        System.out.println(getCode(10, 1).toString());// 数字
        System.out.println(getCode(10, 2).toString());// 小写字母
        System.out.println(getCode(10, 3).toString());// 大写字母
        System.out.println(getCode(10, 4).toString());// 数字+大、小写字母
        System.out.println(getCode(10, 5).toString());// 数字+大、小写字母
        System.out.println(getCode(10, 6).toString());//UUID去掉连字符的32位字符串
    }

    public static String getCode(int passLength, int type) {
        StringBuffer buffer = null;
        StringBuffer sb = new StringBuffer();

        Random random = new Random();
        random.setSeed(new Date().getTime());

        switch (type) {
            case 1:
                buffer = new StringBuffer("0123456789");// 数字
                break;
            case 2:
                buffer = new StringBuffer("abcdefghijklmnopqrstuvwxyz");// 小写字母
                break;
            case 3:
                buffer = new StringBuffer("ABCDEFGHIJKLMNOPQRSTUVWXYZ");// 大写字母
                break;
            case 4:
                buffer = new StringBuffer(
                        "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ");// 数字+小写、大写字母
                break;
            case 5:
                buffer = new StringBuffer(
                        "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789");// 数字+小写、大写字母
                sb.append(buffer.charAt(random.nextInt(buffer.length() - 10)));//26+26+10-10=52->[0-52)位
                passLength -= 1;
                break;
            case 6:
                String s = UUID.randomUUID().toString();
                //随机36位UUID(包括4个连字符位置:8,13,18,23)
                //如:0e6c14a9-eb23-480e-99ad-cf320fc4c843
                //substring(a,b):取到a取不到b
                //sb.append(s.replaceAll("-", ""));
                sb.append(s.substring(0, 8) + s.substring(9, 13) + s.substring(14, 18) + s.substring(19, 23) + s.substring(24));
                //去掉连字符,然后取到32位,如:eee671a5 bd0e 4774 b38a ebf862867fd4
        }
        //如果不是UUID
        if (type != 6) {
            int range = buffer.length();//type对应的buffer的长度[10]或者[26]或者[36]
            for (int i = 0; i < passLength; i++) {//验证码的位数
                sb.append(buffer.charAt(random.nextInt(range)));//依次将对应下标(随机)的字符添加到sb中
                //random.nextInt(range)为[0,10)或者[0,26)中随机一个
                //buffer.charAt(random.nextInt(range))为buffer取对应下标的字符
            }
        }
        return sb.toString();//将字符序列转换为字符串
    }
在C++中,你可以使用以下方法获取UUID(通用唯一标识符): 方法一:使用第三方库(例如boost库)来生成UUID。 首先,确保你的C++项目中已经安装了boost库。然后,可以使用以下代码来生成UUID: ```cpp #include <boost/uuid/uuid.hpp> #include <boost/uuid/uuid_generators.hpp> #include <boost/uuid/uuid_io.hpp> int main() { boost::uuids::random_generator gen; boost::uuids::uuid uuid = gen(); std::cout << uuid << std::endl; return 0; } ``` 这段代码使用了boost库中的`random_generator`类来生成UUID,并通过`uuid()`方法获取随机生成的UUID。然后,可以通过`std::cout`输出UUID。 方法二:使用操作系统提供的API来获取UUID。 在不使用第三方库的情况下,你可以使用操作系统提供的API来获取UUID。以下是在Windows和Linux上获取UUID的示例代码: - Windows: ```cpp #include <iostream> #include <rpc.h> #pragma comment(lib, "Rpcrt4.lib") int main() { UUID uuid; UuidCreate(&uuid); unsigned char* str; UuidToStringA(&uuid, &str); std::cout << str << std::endl; RpcStringFreeA(&str); return 0; } ``` 这段代码使用了Windows提供的`UuidCreate()`函数来生成UUID,并通过`UuidToStringA()`函数将UUID转换为字符串格式,并输出。 - Linux: ```cpp #include <iostream> #include <uuid/uuid.h> int main() { uuid_t uuid; uuid_generate(uuid); char str[37]; uuid_unparse(uuid, str); std::cout << str << std::endl; return 0; } ``` 这段代码使用了Linux提供的`uuid_generate()`函数来生成UUID,并通过`uuid_unparse()`函数将UUID转换为字符串格式,并输出。 这些是获取UUID的两种常见方法,你可以根据自己的需求选择其中一种来实现。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值