Skip to content

Commit

Permalink
Major update
Browse files Browse the repository at this point in the history
  • Loading branch information
hg0428 committed Nov 19, 2023
1 parent fb62139 commit ac49242
Show file tree
Hide file tree
Showing 27 changed files with 247 additions and 155 deletions.
2 changes: 1 addition & 1 deletion PCSS/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

makeid = lambda: hashlib.sha256(
str(
random.randint(-999999999999999999999, 999999999999999999999 + time.time())
random.randint(-2**512, 2**512 + time.time())
).encode()).hexdigest() + hashlib.sha512(str(uuid1()).encode()).hexdigest(
) + str(uuid4()) + str(uuid5(uuid4(), str(uuid1())))

Expand Down
Binary file modified PCSS/__pycache__/__init__.cpython-310.pyc
Binary file not shown.
Binary file modified PCSS/__pycache__/hash.cpython-310.pyc
Binary file not shown.
62 changes: 56 additions & 6 deletions PCSS/hash.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,67 @@
from bitarray import bitarray
from bitarray.util import int2ba
from hashlib import sha256
from random import shuffle, seed


def hash(data: str, length: int = 256) -> str:
def pad_bitarray_left(ba, size=4):
# Calculate the number of bits to pad
pad_size = size - len(ba)

# Create a new bitarray for padding
pad = bitarray(pad_size)
pad.setall(0)

# Prepend the padding to the original bitarray
ba[:0] = pad
return ba


def merge_bitarrays(*bitarrays):
merged_bitarray = bitarray()

# Extend the result bitarray with each bitarray in the list
for ba in bitarrays:
merged_bitarray.extend(ba)
return merged_bitarray


def split_bitarray(ba, block_size=4):
return [
pad_bitarray_left(ba[i:i + block_size], block_size)
for i in range(0, len(ba), block_size)
]


def hash(data: str, length: int = 256, block_size: int = 4) -> str:
"""
Hashes data using the hashing PCSS algorithm.
The length of the hash is supplied as the length arguemnt and it must be divisible by the block size (which defaults to 4).
An increased block size results in increased randomness.
"""
if length % block_size != 0:
raise ValueError("Length must be a multiple of block_size")
encoded = bitarray()
encoded.frombytes(data.encode('utf-8'))
blocks = [encoded[i:i + 4] for i in range(0, len(encoded), 4)]
fourbits = [bitarray(4) for _ in range(length)]
for index, i in enumerate(fourbits):
hash_data = [
encoded,
int2ba(len(data)),
int2ba(length),
int2ba(encoded.count(1))
]
blocks = []
for x in hash_data:
blocks = blocks + split_bitarray(x, block_size)
emptyblocks = [bitarray(block_size) for _ in range(length)]
seed((length + block_size + len(data)) * int(blocks[0].to01(), 2))
for index, i in enumerate(emptyblocks):
i.setall(0)
shuffle(blocks)
for block in blocks:
i ^= block
x = bitarray(blocks[min(index, len(blocks) - 1)])
x.reverse()
i ^= ~x
fourbits[index] = ~i
return ''.join('{:x}'.format(int(i.to01(), 2)) for i in fourbits)
emptyblocks[index] = ~i
return ''.join('{:x}'.format(int(i.to01(), 2))
for i in split_bitarray(merge_bitarrays(*emptyblocks), 4))
74 changes: 0 additions & 74 deletions Projxon_Cyber_Security_System.egg-info/PKG-INFO

This file was deleted.

12 changes: 0 additions & 12 deletions Projxon_Cyber_Security_System.egg-info/SOURCES.txt

This file was deleted.

49 changes: 33 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ from PCSS import encrypt, decrypt, makeid, bitarray_to_text
from PCSS.hash import hash

print(f"""
Hash of Hi!: {hash("Hi!")}
Hash of Hello_World!: {hash("Hello_World!")}
Hash of Hello_World! : {hash("Hello_World! ")}
Hash of Hello World: {hash("Hello World")}
Expand All @@ -29,36 +30,35 @@ Hash of !#!#!#!#!#!#!#!#: {hash("!#!#!#!#!#!#!#!#")}
""")
# Output:
"""
Hash of Hello_World!: 133e15fbd573507fd63714d032547698badcfe1032547698badcfe1032547698badcfe1032547698badcfe1032547698badcfe1032547698badcfe1032547698badcfe1032547698badcfe1032547698badcfe1032547698badcfe1032547698badcfe1032547698badcfe1032547698badcfe1032fe1032547698badcfe1032
Hash of Hello_World! : 6ac3ec8eac8ea90aafcaeda5832107654ba98fedc32107654ba98fedc32107654ba98fedc32107654ba98fedc32107654ba98fedc32107654ba98fedc32107654ba98fedc32107654ba98fedc32107654ba98fedc32107654ba98fedc32107654ba98fedc32107654ba9854ba98fedc32107654bfeecddc32107654ba98fedc3
Hash of Hello World: 026b0426c4c941a2c762056789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123ef0123456789abcdef0455766789abcdef012345182439abcdef01234567
Hash of Password#123: 7ad07a98bc5ffa9e967052b89efcd23016745ab89efcd23016745ab89efcd23016745ab89efcd23016745ab89efcd23016745ab89efcd23016745ab89efcd23016745ab89efcd23016745ab89efcd23016745ab89efcd2301cd23016745ab89efcd26775445ab89efcd2301673a061b89efcd230164f5bacd23016745ab89efb
Hash of AAAAAAAAAAAA: 4fa183e5c7290b6d4fa183e56789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789ab6789abcdef012345678cddfeef0123456789abcd90acb123456789abce5f106789abcdef0123d6e0f789abcdef01234567
Hash of !#!#!#!#!#!#!#!#: 5a781e34d2f096bc5a781e34d2f096bcdef89ab45670123cdef89ab45670123cdef89ab45670123cdef89ab45670123cdef89ab45670123cdef89ab45670123cdef23cdef89ab45670123c899baab45670123cdef89d4e8f5670123cdef8a1b5423cdef89ab456792a4b3cdef89ab456700d165ef89ab45670123cdef7067ccc
Hash of Hi!: 32aa253325323f2f7a22f52313272b77335b11b25327aab12772a3522522bf77322223233227222ff2ff3b33332a27252b3ff323127b2335ba752f2a1f3b22122b2b23522522af3f22f522a735f273112332f722b227f122b22111f22222532f75327172332271377a2ffb3322232ba22f3bf2223322f73ab12f52f52a1ab332
Hash of Hello_World!: 0c1c55cb50099c5305199d9500932925530517975755d00b20cc222909550305039275050005905205d7770530c5c0953d50cd9559155729c39101352395c7cc5971b700d925d3d5570500d1c725992c957d500b555950352521dd7b3c505d759d057377005550252b552335d0171071092dd5000959005d53c75170520c5297
Hash of Hello_World! : 117eb51b771a2557be7fbd710b2231e1e1775227ff13b217e551137e7f55977ddee73e70b0922232a21e773777bb1e155170150172b9713f7f273a3d32737db5ee29af755f75711fb7bf59729efadb12eff722177777e2725e2be7129055075050b55535270302e252213ef752d10b7772b7ba27e5772773732f1f7579777107
Hash of Hello World: d9ed49a56eea69bc42ce69e89c25bc828bee5d7ece576e6bbb76b94eee779e575daba4be8ece2e8899e2e2bec978b87d7d79a972225e246b687ede897bb556a7e6e8abaebaee6ee282eb45beee4ee5858a8e2e2bcbdbcb84ceee845ee565769cde5ee99958a296e9eee87d774ce82beb8eeaeedceeeb8ec4b8d929e88eebcbea
Hash of Password#123: acae4c46684ec4cefccce6c61c2e6c330fe4fcc8c12cec2004a4acaec3ea2c0448e8ee464ca2ceee6ca2eea62acaa230e6feccaeadcefeec2ee318ccc382ec6c4ecdee46e2cc664ece2eeaeee424c1eee6cece68e2e0e64ee46dfee8ceccc6eccfc26eecc6eeeceee28aa2a41c1f22ac63ee46660ec8c48c2e4ce6aecea4aeea
Hash of AAAAAAAAAAAA: b0a0aa00abba00a80a0aaa0a0b089a0aa8ba080aaaaa0bb00b00909a0aaa08ba0800aa0a000aa0a0baa8a00a8b0a000a80ab0a0aaa0aa09a08a00a8a98aa0000aa0a0abbaa0a080aaaba0b000a0aaa90aaa0ab00aaaaab8a9a90aaa080a0aaaaaa0aa88ab0aaa09a90aa088aa0a0a0aa0a00aa00baaab0aaa808a0aba000a008
Hash of !#!#!#!#!#!#!#!#: beab626ee6e6b66e666bbea622ae26622efafaee2e2b6ae66eeeeeee22ae6626aea62beeee26eefb222ee622ea6eee66ea66e2ee22e6a66e2ae2666f6ee6fee6e2eeb6226622e2a6e2ebf666ee62a2bb2eeeee6a26eee6e2eeeee262ae2e6afa66e2eee6e66e62e626e2aa22ebbea6ee26e6a2abbe26eee62226eeee6a6b6e62
"""
x = 'Hello Everyone!'
print('Hash works?', hash(x) == hash(x)) # True

## Encryption

# Encode it so that we have a bytes object to pass to encrypt
text = "Hello World!"
key = "key"
# Encode it so that we have a bytes object to pass to encrypt

encoded_text = text.encode()
encoded_key = key.encode()

# Encrypt the text
encrypted_bitarray = encrypt(encoded_text, encoded_key)
encrypted_bitarray = encrypt(text, key)
print(encrypted_bitarray)
print(bitarray_to_text(encrypted_bitarray))

# Decrypt the bitarray
decrypted_bitarray = decrypt(encrypted_bitarray, encoded_key)
decrypted_bitarray = decrypt(encrypted_bitarray, key)

# Convert it back to a string.
decrypted_text = bitarray_to_text(decrypted_bitarray)

print(decrypted_text, text == decrypted_text)
# Hello World! True

```


Expand All @@ -70,6 +70,23 @@ print(makeid())
# 3fbe99d84c1b9255565c79d758bebba6e011ba2ca0795c5c50d22519adaf99050e74cb261933091fc4c52d6a7581c3681557e4adf495b1cd1314579e962791efc6d5ceb9133de75cd25a20a99d50cf4b7bf927f61dfa298d69d573da101f26a973241224-a5eb-49fa-828b-f86b89d9a4745480679b-4d20-5b8a-a72b-268877dc086a
# ids will vary.
```
These ids are garunteed to be unique, the chance of collision is about 1 / (2.42721841x10^229).
For refernce, the number of atoms in the universe is estimated to be 10^78.
These ids are garunteed to be unique; the chance of collision is about 1 / (2.42721841x10^229).
For reference, the number of atoms in the universe is estimated to be 10^78.


# Documentation:
## PCSS
### `encrypt(data: Union[str, bytes, bitarray], key: Union[str, bytes, bitarray, None]=None, final_key: Union[bitarray, None]=None, rounds=None, salt=None) -> bitarray`
Encrypts data using an advanced method including XOR encrytion and byte and bit reversal.
`salt` will defualt to the key.

### `decrypt(encrypted_data: Union[str, bytes, bitarray], key: Union[str, bytes, bitarray, None]=None, final_key: Union[bitarray, None]=None, rounds=None, salt=None) -> bitarray`
Decrypts data that was encrypted using the PCSS.encrypt function. Parameters that do not match will generate an incorrect output.

### `makeid()`
Generates a long and secure id.
These ids are garunteed to be unique; the chance of collision is about 1 / (2.42721841x10^229).
For reference, the number of atoms in the universe is estimated to be 10^78.

## PCSS.hash
### `hash(data: str, length: int = 256, block_size: int = 4) -> str`
Binary file removed dist/Projxon Cyber Security System-0.0.3.tar.gz
Binary file not shown.
Binary file removed dist/Projxon Cyber Security System-0.0.4.tar.gz
Binary file not shown.
Binary file removed dist/Projxon Cyber Security System-0.0.5.tar.gz
Binary file not shown.
Binary file removed dist/Projxon Cyber Security System-0.1.1.tar.gz
Binary file not shown.
Binary file removed dist/Projxon Cyber Security System-0.1.tar.gz
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file added dist/projxon_cyber_security_system-0.1.1.tar.gz
Binary file not shown.
43 changes: 21 additions & 22 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,39 +2,40 @@
from PCSS.hash import hash

print(f"""
Hash of Hello_World!: {hash("Hello_World!")}
Hash of Hello_World! : {hash("Hello_World! ")}
Hash of Hello World: {hash("Hello World")}
Hash of Password#123: {hash("Password#123")}
Hash of AAAAAAAAAAAA: {hash("AAAAAAAAAAAA")}
Hash of !#!#!#!#!#!#!#!#: {hash("!#!#!#!#!#!#!#!#")}
Hash of Hi!: {hash("Hi!", 256)}
Hash of Hello_World!: {hash("Hello_World!", 256)}
Hash of Hello_World! : {hash("Hello_World! ", 256)}
Hash of Hello World: {hash("Hello World", 256)}
Hash of Password#123: {hash("Password#123", 256)}
Hash of AAAAAAAAAAAA: {hash("AAAAAAAAAAAA", 256)}
Hash of !#!#!#!#!#!#!#!#: {hash("!#!#!#!#!#!#!#!#", 256)}
""")

# Output:
"""
Hash of Hello_World!: 133e15fbd573507fd63714d032547698badcfe1032547698badcfe1032547698badcfe1032547698badcfe1032547698badcfe1032547698badcfe1032547698badcfe1032547698badcfe1032547698badcfe1032547698badcfe1032547698badcfe1032547698badcfe1032547698badcfe1032fe1032547698badcfe1032
Hash of Hello_World! : 6ac3ec8eac8ea90aafcaeda5832107654ba98fedc32107654ba98fedc32107654ba98fedc32107654ba98fedc32107654ba98fedc32107654ba98fedc32107654ba98fedc32107654ba98fedc32107654ba98fedc32107654ba98fedc32107654ba98fedc32107654ba9854ba98fedc32107654bfeecddc32107654ba98fedc3
Hash of Hello World: 026b0426c4c941a2c762056789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123ef0123456789abcdef0455766789abcdef012345182439abcdef01234567
Hash of Password#123: 7ad07a98bc5ffa9e967052b89efcd23016745ab89efcd23016745ab89efcd23016745ab89efcd23016745ab89efcd23016745ab89efcd23016745ab89efcd23016745ab89efcd23016745ab89efcd23016745ab89efcd2301cd23016745ab89efcd26775445ab89efcd2301673a061b89efcd230164f5bacd23016745ab89efb
Hash of AAAAAAAAAAAA: 4fa183e5c7290b6d4fa183e56789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789ab6789abcdef012345678cddfeef0123456789abcd90acb123456789abce5f106789abcdef0123d6e0f789abcdef01234567
Hash of !#!#!#!#!#!#!#!#: 5a781e34d2f096bc5a781e34d2f096bcdef89ab45670123cdef89ab45670123cdef89ab45670123cdef89ab45670123cdef89ab45670123cdef89ab45670123cdef23cdef89ab45670123c899baab45670123cdef89d4e8f5670123cdef8a1b5423cdef89ab456792a4b3cdef89ab456700d165ef89ab45670123cdef7067ccc
Hash of Hi!: 32aa253325323f2f7a22f52313272b77335b11b25327aab12772a3522522bf77322223233227222ff2ff3b33332a27252b3ff323127b2335ba752f2a1f3b22122b2b23522522af3f22f522a735f273112332f722b227f122b22111f22222532f75327172332271377a2ffb3322232ba22f3bf2223322f73ab12f52f52a1ab332
Hash of Hello_World!: 0c1c55cb50099c5305199d9500932925530517975755d00b20cc222909550305039275050005905205d7770530c5c0953d50cd9559155729c39101352395c7cc5971b700d925d3d5570500d1c725992c957d500b555950352521dd7b3c505d759d057377005550252b552335d0171071092dd5000959005d53c75170520c5297
Hash of Hello_World! : 117eb51b771a2557be7fbd710b2231e1e1775227ff13b217e551137e7f55977ddee73e70b0922232a21e773777bb1e155170150172b9713f7f273a3d32737db5ee29af755f75711fb7bf59729efadb12eff722177777e2725e2be7129055075050b55535270302e252213ef752d10b7772b7ba27e5772773732f1f7579777107
Hash of Hello World: d9ed49a56eea69bc42ce69e89c25bc828bee5d7ece576e6bbb76b94eee779e575daba4be8ece2e8899e2e2bec978b87d7d79a972225e246b687ede897bb556a7e6e8abaebaee6ee282eb45beee4ee5858a8e2e2bcbdbcb84ceee845ee565769cde5ee99958a296e9eee87d774ce82beb8eeaeedceeeb8ec4b8d929e88eebcbea
Hash of Password#123: acae4c46684ec4cefccce6c61c2e6c330fe4fcc8c12cec2004a4acaec3ea2c0448e8ee464ca2ceee6ca2eea62acaa230e6feccaeadcefeec2ee318ccc382ec6c4ecdee46e2cc664ece2eeaeee424c1eee6cece68e2e0e64ee46dfee8ceccc6eccfc26eecc6eeeceee28aa2a41c1f22ac63ee46660ec8c48c2e4ce6aecea4aeea
Hash of AAAAAAAAAAAA: b0a0aa00abba00a80a0aaa0a0b089a0aa8ba080aaaaa0bb00b00909a0aaa08ba0800aa0a000aa0a0baa8a00a8b0a000a80ab0a0aaa0aa09a08a00a8a98aa0000aa0a0abbaa0a080aaaba0b000a0aaa90aaa0ab00aaaaab8a9a90aaa080a0aaaaaa0aa88ab0aaa09a90aa088aa0a0a0aa0a00aa00baaab0aaa808a0aba000a008
Hash of !#!#!#!#!#!#!#!#: beab626ee6e6b66e666bbea622ae26622efafaee2e2b6ae66eeeeeee22ae6626aea62beeee26eefb222ee622ea6eee66ea66e2ee22e6a66e2ae2666f6ee6fee6e2eeb6226622e2a6e2ebf666ee62a2bb2eeeee6a26eee6e2eeeee262ae2e6afa66e2eee6e66e62e626e2aa22ebbea6ee26e6a2abbe26eee62226eeee6a6b6e62
"""
x = 'Hello Everyone!'
print('Hash works?', hash(x) == hash(x)) # True

## Encryption

# Encode it so that we have a bytes object to pass to encrypt
text = "Hello World!"
key = "key"
# Encode it so that we have a bytes object to pass to encrypt

encoded_text = text.encode()
encoded_key = key.encode()

# Encrypt the text
encrypted_bitarray = encrypt(encoded_text, encoded_key)
encrypted_bitarray = encrypt(text, key)
print(encrypted_bitarray)
print(bitarray_to_text(encrypted_bitarray))
# bitarray('00110110101010000111000011100000110100000110001010100110010000101000000000100000101000000010100010001100010011000010010001101100101011001010110011001100100111000010010010000110010011000110101010101100001001000100001011100010110011100111011000101110011000100101111001101010000111000001011010011110111000100001111001011110011011100110110011101010001101100010110001010110001100101111011010011100111100101010111010011100000100100101001001100010010010100111001001011110100111001001011011101100010010100110111001010010110011000100001010000110000100100101101000101100111101101100110000011100110000100100110011110010001011101110110011001100000110101000110001110110111000101111010011110110101011100100101010101100110101101101011000101010110010101011001001110100010001101000101010101100011011001011011001101110100100101010101011001110101011000011011011101100100001101111011001011110010010101010110011101110')

# Decrypt the bitarray
decrypted_bitarray = decrypt(encrypted_bitarray, encoded_key)
decrypted_bitarray = decrypt(encrypted_bitarray, key)

# Convert it back to a string.
decrypted_text = bitarray_to_text(decrypted_bitarray)
Expand All @@ -45,5 +46,3 @@
print(makeid())
# 3fbe99d84c1b9255565c79d758bebba6e011ba2ca0795c5c50d22519adaf99050e74cb261933091fc4c52d6a7581c3681557e4adf495b1cd1314579e962791efc6d5ceb9133de75cd25a20a99d50cf4b7bf927f61dfa298d69d573da101f26a973241224-a5eb-49fa-828b-f86b89d9a4745480679b-4d20-5b8a-a72b-268877dc086a
# ids will vary.

# These ids are garunteed to be unque, the chance of collision is under 1 / (2^256)
Loading

0 comments on commit ac49242

Please sign in to comment.