您的位置:首页 > 其它

用TCL编写了一个生成密码字典的小测试程序,共享一下

2006-05-30 10:49 921 查看
闲着无聊,编写了一个生成密码字典的小程序,做为学习tcl的一个阶段性总结。
可以生成包含dictChar任意组合的字符,密码长度也可以设置。
在debian下使用tclsh8.4测试通过。

#! /usr/bin/tclsh

#************************************************
# Password Dictionary Generator
# Just for learning, by easwy, Mar 31, 2006
#
# len - the length of password
#
# RETURN
#    none, password group in file $dictName
#************************************************
proc genDict {len} {
    # chars in password
    set dictChar "abcdefghijklmnopqrstuvwxyz"
    # count of chars
    set charCnt [string length $dictChar]
    # last item's index in array a
    set last [expr "$len - 1"]
    # dictionary file name
    set dictName "passwd.txt"

    # initial array a
    for {set i 0} {$i < $len} {incr i} {
        set a($i) 0
    }

    # open dictionary file
    set dictFile [open "passwd.txt" w]

    # starting...
    while {true} {
        # construct new passwd
        set passwd ""
        for {set i 0} {$i < $len} {incr i} {
            # append char which index is $a($i) to passwd str
            set passwd "$passwd[string index $dictChar $a($i)]"
        }

        # output passwd
        puts $dictFile $passwd

        # incr last char's index
        incr a($last)

        # update all indices
        for {set i $last} {$i > 0} {incr i -1} {
            if {$a($i) >= $charCnt} {
                set a($i) 0
                set ind [expr "$i - 1"]
                incr a($ind)
            }
        }

        # exit
        if {$a(0) >= $charCnt} {break}
    }

    close $dictFile
}

# generate passwd string, len 3
genDict 3
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
相关文章推荐