File: crypt.py

package info (click to toggle)
codeville 0.8.0-2.1
  • links: PTS
  • area: main
  • in suites: buster, stretch
  • size: 1,140 kB
  • sloc: python: 10,335; ansic: 89; sh: 62; makefile: 25
file content (17 lines) | stat: -rw-r--r-- 587 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# Written by Ross Cohen
# see LICENSE.txt for license information

from entropy import string_to_long, long_to_string
import hashlib

def crypt(text, key, counter=0L):
    keylen, length = len(key), len(text)
    pos, cyphertext = 0, []
    while pos < length:
        scounter = long_to_string(counter, keylen)
        hash = hashlib.sha1("ctr mode crypt" + key + scounter).digest()
        for i in xrange(min(length-pos, len(hash))):
            cyphertext.append(chr(ord(hash[i]) ^ ord(text[pos])))
            pos += 1
        counter += 1
    return (''.join(cyphertext), counter)