Skip to content

Commit

Permalink
change default sha256 to interpret string as hex. update tests to acc…
Browse files Browse the repository at this point in the history
…ount for both hex and ascii strings
  • Loading branch information
danielsuo committed Nov 25, 2014
1 parent b7985c9 commit b944c20
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 17 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ First, we're going to implement a thin-client wallet.
## Public key distribution
- 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
- Elliptic Curve DSA [ref](https://github.com/bitcoin/secp256k1/blob/master/src/secp256k1.c)
- Wallet Interchange Format [ref](https://en.bitcoin.it/wiki/WIF)

## Signing program
- TBD
Expand Down
32 changes: 21 additions & 11 deletions src/Crypto/SHA2.jl
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,27 @@ function sha256_transform!(state, block)
return
end

function sha256(msg::ASCIIString)
function sha256(msg::ASCIIString; is_hex=true)

if is_hex
len = int(length(msg) / 2)
result = zeros(Uint8, len)
for i = 1:len
result[i] = uint8(parseint(msg[2 * i - 1: 2 * i],16))
end
msg = result
else
# We only want byte array literal (i.e., character array)
msg = msg.data
end

# Get original length and bit lengths
len = length(msg)
bitlen = len * 8

# Append the bit '1' to the message.
append!(msg, [0x80])

# First 32 bits of the fractional parts of the square roots of the first 8
# primes, 2 through 19. A sample generation function below:
#
Expand All @@ -161,16 +181,6 @@ function sha256(msg::ASCIIString)
state = [0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a,
0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19]

# We only want byte array literal (i.e., character array)
msg = msg.data

# Get original length and bit lengths
len = length(msg)
bitlen = len * 8

# Append the bit '1' to the message.
append!(msg, [0x80])

# Divide up message into blocks of BLOCK_SIZE = 512 bits
# and run through transformation
while length(msg) >= BLOCK_SIZE
Expand Down
12 changes: 8 additions & 4 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,13 @@ 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("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 Coin.Crypto.SHA2.sha256("a", is_hex=false) == "ca978112ca1bbdcafac231b39a23dc4da786eff8147c4e72b9807785afee48bb"
@test Coin.Crypto.SHA2.sha256("Scientific progress goes 'boink'", is_hex=false) == "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.", is_hex=false) == "a5d8cfb99203ae8cd0c222e8aaef815a7a53493f650c5dec0d73de7f912e91f2"

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

# Testing hex strings
@test Coin.Crypto.SHA2.sha256("800C28FCA386C7A227600B2FE50B7CAE11EC86D3BF1FBE471BE89827E19D72AA1D") == "8147786c4d15106333bf278d71dadaf1079ef2d2440a4dde37d747ded5403592"
@test Coin.Crypto.SHA2.sha256("8147786c4d15106333bf278d71dadaf1079ef2d2440a4dde37d747ded5403592") == "507a5b8dfed0fc6fe8801743720cedec06aa5c6fca72b07c49964492fb98a714"

0 comments on commit b944c20

Please sign in to comment.