#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2015
    Posts
    38
    Rep Power
    4

    For Loop with SHA256 Hash


    I'm working on trying to learn Python coming from a PHP background. Everything was going fine until I tried to use a SHA256 hash function. I'm trying to loop over a set of integers and hash them.

    I used the following:

    Code:
    import hashlib
    for num in range(1,2500):
        str(num)
        hash=hashlib.sha256(num.encode()).hexdigest()
        print(hash)
    But I get a traceback in IDLE:
    Traceback (most recent call last):
    File "<pyshell#3>", line 2, in <module>
    hash=hashlib.sha256(num.encode()).hexdigest()
    AttributeError: 'int' object has no attribute 'encode'
    This runs with one line in PHP, so I'm not quite getting why this is so difficult:
    PHP Code:
    <?php
    for($num=0$num 2500$num++){echo hash("sha256"$num).'<br/>' ;}
    ?>
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    May 2009
    Posts
    667
    Rep Power
    40
    You have to catch the return from str

    Code:
        num_str=str(num)
        hash=hashlib.sha256(num_str.encode()).hexdigest()

    Comments on this post

    • AJinNYC2112 agrees
    • Will-O-The-Wisp agrees
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2015
    Posts
    38
    Rep Power
    4
    Thanks. I though the str() function passed back a self updated variable with the same name. All of the examples I've found showed just using the str() function without needing to pass it back to a variable, but I guess that was to print it right out in IDLE

IMN logo majestic logo threadwatch logo seochat tools logo