Skip to content

Commit

Permalink
fix lpad bug in SHA256 final digest concatenation; leading 0s were ge…
Browse files Browse the repository at this point in the history
…tting left off. Add tests to handle
  • Loading branch information
danielsuo committed Nov 25, 2014
1 parent 61c7ad0 commit b7985c9
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 3 deletions.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@ A (self-educational, incomplete, and likely incorrect) library for working with
First, we're going to implement a thin-client wallet.

## Public key distribution
- TBD
- Base58 encoding / decoding [ref](https://github.com/bitcoin/bitcoin/blob/master/src/base58.cpp)
- RIPEMD-160 [ref](https://github.com/bitcoin/bitcoin/blob/master/src/crypto/ripemd160.cpp)
- Elliptic Curve DSA
- Wallet Interchange Format

## Signing program
- TBD
Expand Down
2 changes: 1 addition & 1 deletion src/Crypto/SHA2.jl
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ function sha256(msg::ASCIIString)
end

# Assemble digest and return
return reduce((x, y) -> string(x, y), map(x -> hex(uint32(x)), state))
return reduce((x, y) -> string(x, y), map(x -> lpad(hex(uint32(x)), 8, "0"), state))
end

end # module SHA256
6 changes: 5 additions & 1 deletion test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,8 @@ using Base.Test

# SHA2 tests
@test Coin.Crypto.SHA2.sha256("a") == "ca978112ca1bbdcafac231b39a23dc4da786eff8147c4e72b9807785afee48bb"
@test Coin.Crypto.SHA2.sha256("Scientific progress goes 'boink'") == "2f2ba2a09a66771bf1fdf541af6e9db4b443145f9935ddd5d4c323c21a8bdcee"
@test Coin.Crypto.SHA2.sha256("Scientific progress goes 'boink'") == "2f2ba2a09a66771bf1fdf541af6e9db4b443145f9935ddd5d4c323c21a8bdcee"
@test Coin.Crypto.SHA2.sha256("I'd hold you up to say to your mother, 'this kid's gonna be the best kid in the world. This kid's gonna be somebody better than anybody I ever knew.' And you grew up good and wonderful. It was great just watching you, every day was like a privilege. Then the time come for you to be your own man and take on the world, and you did. But somewhere along the line, you changed. You stopped being you. You let people stick a finger in your face and tell you you're no good. And when things got hard, you started looking for something to blame, like a big shadow. Let me tell you something you already know. The world ain't all sunshine and rainbows. It's a very mean and nasty place and I don't care how tough you are it will beat you to your knees and keep you there permanently if you let it. You, me, or nobody is gonna hit as hard as life. But it ain't about how hard ya hit. It's about how hard you can get hit and keep moving forward. How much you can take and keep moving forward. That's how winning is done! Now if you know what you're worth then go out and get what you're worth. But ya gotta be willing to take the hits, and not pointing fingers saying you ain't where you wanna be because of him, or her, or anybody! Cowards do that and that ain't you! You're better than that! I'm always gonna love you no matter what. No matter what happens. You're my son and you're my blood. You're the best thing in my life. But until you start believing in yourself, ya ain't gonna have a life. Don't forget to visit your mother.") == "a5d8cfb99203ae8cd0c222e8aaef815a7a53493f650c5dec0d73de7f912e91f2"

# Test > 448 bits (> 56 characters)
@test Coin.Crypto.SHA2.sha256("asdfghjkqwasdfghjkqwasdfghjkqwasdfghjkqwasdfghjkqwasdfghjkqw") == "07a95e647687cf0e8cd3d0ca78c9cc9b120ab41497f5f3be912c6c3f1ecd3a31"

0 comments on commit b7985c9

Please sign in to comment.