简体   繁体   中英

Check for charset size with AppleScript

I'm trying to make a script that based on charset size and guesses per second, makes an estimate on how long it would take to crack a password that the user inputs in a box. So far I can only get it to check for numbers, by trying to add the text entered in the box to itself. How can I get it to check if the charset is

...only alpha, then alpha-numeric, then mixed-alpha, then mixed-alpha-numeric, then only symbolic, then alpha-symbolic, then mixed-alpha-symbolic, then mixed-alpha-numeric-symbolic, then numeric-symbolic, then alpha-numeric-symbolic, etc.?

I know it's pretty long but...you get the point. (Right?)

I know a way that kind of works, but it's order-sensitive.

set thetext to "asdf"
set alpha to {"a", "b"}
if thetext contains alpha then
    say "yes"
else
    say "no"
end if

But I want to make it order-insensitive.

What you probably want to do is loop through each character in the text. Then, compare that character to the alphabet, to numbers, and to however you define symbolic. Then, keep track of what kinds of things you find.

It looks like the kinds of things you are looking for are alpha, numeric, and symbolic. If you keep track of those three things, you can then construct the combinations listed in your requirement.

set theText to "asdf"

set theAlphabet to "abcdefghijklmnopqrstuvwxyz"
set theNumbers to "0123456789"

set containsAlpha to false
set containsNumber to false
set containsSomethingElse to false

repeat with theCharacter in theText
    if theAlphabet contains theCharacter then
        set containsAlpha to true
    else if theNumbers contains theCharacter then
        set containsNumber to true
    else
        set containsSomethingElse to true
    end if
end repeat

if containsAlpha and (containsNumber or containsSomethingElse) then
    if containsNumber and containsSomethingElse then
        say "The text contains a mix of letters, numbers, and something else."
    else if containsNumber then
        say "The text contains a mix of letters and numbers."
    else
        say "The text contains a mix of letters and something non-numeric."
    end if
else if containsAlpha then
    say "The text contains only characters from the ASCII alphabet."
else if containsNumber and containsSomethingElse then
    say "The text contains a mix of numbers and something non-alphabetic."
else if containsNumber then
    say "The text contains only numbers."
else if containsSomethingElse then
    say "The text contains only non-numeric and non-alphabetic characters."
else
    say "I think there is something wrong with my logic, Dave."
end if

I'm calling the third category “something else” instead of symbolic, because I don't want to presume what your criteria are for symbolic; for that matter, you may have different criteria for the alphabet, too. The comparison is case-insensitive, but will currently count non-ASCII letters are “something else”.

If you want case sensitivity, (giving you four criteria instead of three), you can surround the comparisons with a considering statement . For example:

if theAlphabet contains theCharacter then
    set containsAlpha to true
    considering case
        if theAlphabet contains theCharacter then
            set containsLowerAlpha to true
        end if
        if theUpperAlphabet contains theCharacter then
            set containsUpperAlpha to true
        end if
    end considering
else if theNumbers contains theCharacter then
…

And then, in the talking section, add the new criteria:

else if containsAlpha then
    if containsLowerAlpha and containsUpperAlpha then
        say "The text contains only upper and lower-case characters from the ASCII alphabet."
    else if containsLowerAlpha then
        say "The text contains only lower-case characters from the ASCII alphabet."
    else
        say "The text contains only upper-case characters from the ASCII alphabet."
    end if
else if containsNumber and containsSomethingElse then
…

Obviously, with four considerations instead of three, the complexity jumps considerably. If the speaking section were the real use case, I might consider setting what kinds of letters the text contains as a string before speaking the complexity:

-- set what kind of letters it contains
if containsAlpha then
    if containsLowerAlpha and containsUpperAlpha then
        set letterPhrase to "upper and lower-case letters"
    else if containsLowerAlpha then
        set letterPhrase to "lower-case letters"
    else
        set letterPhrase to "upper-case letters"
    end if
end if

--speak the complexity of the phrase
if containsAlpha and (containsNumber or containsSomethingElse) then
    if containsNumber and containsSomethingElse then
        say "The text contains a mix of " & letterPhrase & ", numbers, and something else."
    else if containsNumber then
        say "The text contains a mix of " & letterPhrase & " and numbers."
    else
        say "The text contains a mix of " & letterPhrase & " and something non-numeric."
    end if
else if containsAlpha then
    say "The text contains only " & letterPhrase & " from the ASCII alphabet."
…

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM