Search code examples
javascriptswiftsha1commoncrypto

Swift SHA1 function without HMAC


i try to get SHA1 working in swift. without using CommonCrypto since it is not default in swift.

please see https://gist.github.com/wdg/f7c8c4088030c59f0f45 (since it's a little to big to post)

if i run a test case in Xcode:

func test_sha1() {
    XCTAssertEqual(sha1("test"), "a94a8fe5ccb19ba61c4c0873d391e987982fbbd3")
}

it will fail, and return 2d891cc96e32c32e8d26704d101208b954f435a5

i got the hash with:

$ php -r "echo sha1('test');echo(PHP_EOL);"
a94a8fe5ccb19ba61c4c0873d391e987982fbbd3

i think the problem is that in the javascript file they use >>> and i don't know what this operator is. So i have used >>.

i hope someone can help.

Thanks in advance


Solution

  • I've got a solution, there was something wrong with the rotate function.

    i have changed the rotate function to

    func rotate(n: Int, _ s: Int) -> Int {
        return ((n << s) & 0xFFFFFFFF) | (n >> (32 - s))
    }
    

    and now it works.