Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature request: ability to hash string #3

Open
miserton opened this issue Dec 20, 2022 · 1 comment
Open

Feature request: ability to hash string #3

miserton opened this issue Dec 20, 2022 · 1 comment
Assignees
Labels
enhancement New feature or request

Comments

@miserton
Copy link

I'll preface this by saying that shasum has been very useful for me, so thank you for creating it in Stata.

This isn't an issue, but a feature request. It would be useful, at times, to be able to hash a single string. There are easy workarounds, such as generating a single variable in a new frame and hashing it, but it would be convenient to do it in a single command. For example, something like:
shasum, string("xxx") md5
which then returns "f561aaf6ef0bf14d4208bb46a4ccb3ad" in r(string).

You could also put in an option for all the hashes:
shasum, string("xxx") all
which returns r(md5), r(sha1), etc. with each of the different hashes.

@mcaceresb mcaceresb self-assigned this Dec 23, 2022
@mcaceresb mcaceresb added the enhancement New feature or request label Dec 23, 2022
@mcaceresb
Copy link
Owner

@miserton There's some workarounds in Stata 16+ that are easier than coding this up in C (which I might get to at some point but it's not very high in my TODO list, unfortunately). So here's two options I can give you that were fairly quick for me to add:

  • Using python (I added the pyhash function to interface with Python's hashlib):
pyhash, hash(md5 sha1 sha256) string(the quick brown fox jumps over the lazy dog)
return list
  • Using frames:
capture program drop hashstring
program hashstring, rclass
    version 16
    syntax anything, string(str asis)
    local okhashes md5 sha1 sha224 sha256 sha384 sha512
    foreach hash of local anything {
        if !`:list hash in okhashes' {
            disp `"unknown hash '`hash''; allowed: `okhashes'"'
            exit 198
        }
    }
    tempname frame
    frame create `frame'
    frame `frame' {
        qui set obs 1
        * mata st_addvar("str" + strofreal(strlen(st_local("string"))), "string")
        mata st_addvar("strL", "string")
        mata st_sstore(1, "string", st_local("string"))
        local genhash
        foreach hash of local anything {
            local genhash `genhash' `hash'(`hash')
        }
        shasum string, `genhash'
        foreach hash of local anything {
            local `hash' = `hash' 
            return local `hash': copy local `hash'
        }
    }
    frame drop `frame'
end
hashstring md5 sha1 sha256, string(the quick brown fox jumps over the lazy dog)
return list

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants