-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
80 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
def is_sha256(content): | ||
"""Make sure this is actually an valid SHA256 hash.""" | ||
digits58 = '0123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz' | ||
for i in range(len(content)): | ||
if not content[i] in digits58: | ||
return False | ||
return len(content) == 64 | ||
|
||
|
||
def is_btc_address(content): | ||
""" | ||
Does simple validation of a bitcoin-like address. | ||
Source: http://bit.ly/17OhFP5 | ||
param : address : an ASCII or unicode string, of a bitcoin address. | ||
returns : boolean, indicating that the address has a correct format. | ||
""" | ||
|
||
# The first character indicates the "version" of the address. | ||
chars_ok_first = "123" | ||
# alphanumeric characters without : l I O 0 | ||
chars_ok = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz" | ||
|
||
# We do not check the high length limit of the address. | ||
# Usually, it is 35, but nobody knows what could happen in the future. | ||
if len(content) < 27: | ||
return False | ||
# Changed from the original code, we do want to check the upper bounds | ||
elif len(content) > 35: | ||
return False | ||
elif content[0] not in chars_ok_first: | ||
return False | ||
|
||
# We use the function "all" by passing it an enumerator as parameter. | ||
# It does a little optimization : | ||
# if one of the character is not valid, the next ones are not tested. | ||
return all((char in chars_ok for char in content[1:])) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import unittest | ||
from dataserv.Validator import is_sha256, is_btc_address | ||
|
||
|
||
class ValidatorTest(unittest.TestCase): | ||
def test_valid_address(self): | ||
addr1 = '191GVvAaTRxLmz3rW3nU5jAV1rF186VxQc' | ||
addr2 = '191GVvAaTRxLmz3rW3nU5jAV1rF186VxQc9999ghjfghj99' | ||
addr3 = 'not valid address' | ||
addr4 = 'not valid &address' | ||
addr5 = '791GVvAaTRxLmz3rW3nU5jAV1rF186VxQc' | ||
|
||
self.assertTrue(is_btc_address(addr1)) | ||
self.assertFalse(is_btc_address(addr2)) | ||
self.assertFalse(is_btc_address(addr3)) | ||
self.assertFalse(is_btc_address(addr4)) | ||
self.assertFalse(is_btc_address(addr5)) | ||
|
||
def test_valid_sha256(self): | ||
valid_hash = '9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08' | ||
self.assertTrue(is_sha256(valid_hash)) | ||
|
||
invalid_hash = 'notarealhash' | ||
self.assertFalse(is_sha256(invalid_hash)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
import RandomIO | ||
|
||
path = RandomIO.RandomIO('seed string').genfile(50) | ||
with open(path,'rb') as f: | ||
print(f.read()) | ||
with open(path, 'rb') as f: | ||
print(f.read()) |