编辑代码

#include <stdio.h>
#include <string.h>

void generate_password(const char *mac, char *password) {
    char cleaned[13];
    int j = 0;
    for (int i = 0; mac[i]; i++) {
        if (mac[i] != ':') cleaned[j++] = toupper(mac[i]);
    }
    cleaned[j] = '\0';

    // 简易哈希计算
    unsigned long hash = 5381;
    for (int i = 0; cleaned[i]; i++) {
        hash = ((hash << 5) + hash) + cleaned[i];
    }

    // 生成字母部分
    char letters[5] = {0};
    unsigned int temp = hash;
    for (int i = 3; i >= 0; i--) {
        letters[i] = 'a' + (temp % 26);
        temp /= 26;
    }

    // 生成数字部分
    unsigned int digits = 0;
    for (int i = 0; i < j; i++) {
        digits = digits * 31 + cleaned[i];
    }
    digits %= 10000;

    sprintf(password, "%.4s%04u", letters, digits);
}

/* 测试用例*/
int main() {
    char mac[] = "";
    char pwd[9];
    generate_password(mac, pwd);
    printf("Password: %s\n", pwd);  // 示例输出:abcd1234
    return 0;
}