C++ 字符串处理5-手机号邮箱如何脱敏处理

1. 关键词

关键词:

C++ 字符串处理 分割字符串 连接字符串 跨平台

应用场景:

有些重要信息需要保密,比如手机号、邮箱等,如何在不影响用户阅读的情况下,将这些信息脱敏处理,以保障用户的隐私安全。

2. strutil.h

#pragma once

#include <string>

namespace cutl
{
    /**
     * @brief Desensitizing a string by replacing some characters with '*'.
     *
     * @param str the string to be desensitized.
     * @return std::string the desensitized string.
     */
    std::string desensitizing(const std::string &str);
} // namespace cutl

3. strutil.cpp

#include <cctype>
#include <algorithm>
#include "strutil.h"

namespace cutl
{
    // 字符串脱敏处理
    std::string desensitizing(const std::string &str)
    {
        std::string result;
        // 只打印前1/4和后1/4的内容,中间用*表示
        if (str.empty())
        {
            result = "";
        }
        else if (str.length() == 1)
        {
            result = "*";
        }
        else if (str.length() == 2)
        {
            result = str.substr(0, 1) + std::string(str.length() - 1, '*');
        }
        else if (str.length() <= 6)
        {
            result = str.substr(0, 2) + std::string(str.length() - 2, '*');
        }
        else if (str.length() < 10)
        {
            result = str.substr(0, 2) + std::string(str.length() - 4, '*') + str.substr(str.length() - 2, 2);
        }
        else if (str.length() < 16)
        {
            // 长度控制在最长12位,中间×不超过6
            auto startCount = (str.length() - 6) > 6 ? 6 : (str.length() - 6);
            result = str.substr(0, 3) + std::string(startCount, '*') + str.substr(str.length() - 3, 3);
        }
        else
        {
            // 长度控制在最长12位
            result = str.substr(0, 4) + std::string(4, '*') + str.substr(str.length() - 4, 4);
        }
        return result;
    }
} // namespace cutl

4. 测试代码

#include "common.hpp"
#include "strutil.h"

void TestDesensitizing()
{
    PrintSubTitle("desensitizing");

    std::string password = "2515774";
    std::cout << "password: " << cutl::desensitizing(password) << std::endl;
    std::string phone = "18500425678";
    std::cout << "phone: " << cutl::desensitizing(phone) << std::endl;
}

5. 运行结果

-------------------------------------------desensitizing--------------------------------------------
password: 25***74
phone: 185*****678

6. 源码地址

更多详细代码,请查看本人写的C++ 通用工具库: common_util, 本项目已开源,代码简洁,且有详细的文档和Demo。

相关推荐

  1. C++ 字符串处理5-手机邮箱如何脱敏处理

    2024-06-15 15:54:03       34 阅读
  2. C#基础|StringBuilder字符串如何高效处理

    2024-06-15 15:54:03       41 阅读
  3. c++处理字符串

    2024-06-15 15:54:03       33 阅读
  4. C语言基础之字符串处理函数

    2024-06-15 15:54:03       35 阅读
  5. C++语言学习(四)—— 字符串处理函数

    2024-06-15 15:54:03       36 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-06-15 15:54:03       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-06-15 15:54:03       106 阅读
  3. 在Django里面运行非项目文件

    2024-06-15 15:54:03       87 阅读
  4. Python语言-面向对象

    2024-06-15 15:54:03       96 阅读

热门阅读

  1. MongoDB 正则表达式

    2024-06-15 15:54:03       27 阅读
  2. C#中数组ProtoBuf使用问题

    2024-06-15 15:54:03       27 阅读
  3. js字符串域名把域名前缀剪切掉

    2024-06-15 15:54:03       20 阅读
  4. Html_Css问答集(6)

    2024-06-15 15:54:03       27 阅读
  5. python的random模块三choices和shuffle()

    2024-06-15 15:54:03       28 阅读
  6. 表的删除与更新

    2024-06-15 15:54:03       31 阅读
  7. SQL Auto Increment

    2024-06-15 15:54:03       19 阅读
  8. select简单查询

    2024-06-15 15:54:03       29 阅读