GRAPHIC: New Entity Operations™ Alpha Logo

EntityScript



core_hash




# -*- coding: utf-8 -*-
"""
COPYRIGHT (C) 2020-2023 NEW ENTITY OPERATIONS INC. ALL RIGHTS RESERVED
INSTANCE: core_hash
MODIFIED: 2023/05/30
OVERVIEW:

core_hash provides hash routines to use within C.ORE

Import the hashing lookup, storage, and inspection tool to understand entities

"""
__version__ = "0.0.7"
__author__ = "Ryan McKenna"
__copyright__ = "Copyright (C) 2020-2023 New Entity Operations Inc."
__credits__ = [
 "Ryan McKenna",
 "New Entity Operations Inc.", "New Entity Operations, LLC"]
__email__ = "Operator@NewEntityOperations.com"
__license__ = "New Entity License"
__maintainer__ = "Ryan McKenna"
__status__ = "Production"

## MODE-> facilities
from MODE.facilities import (allowed_asset_types, BUCKET_HASHED_SLUGS,
 IMAGE_ALLOWED, md5, oFo, MODULE_MATCHER_MAIN, Path, PATH_INSTANCE, randint,
 sha1, sha256, sha512)

## MODE.debug_steps
from MODE.debug_steps import DEBUG_STEPS

# Imports: Custom
from core_middlelayer import (DIRDATA, DIRPHOTO, ENTITYMANIFEST, HASHNODE,
 SEARCHPARAMS, SLUGHASH, THREADMEDIADIR, THREADDEHASH)

## Define lib-operators, convert the preset to a list
for (key, value) in enumerate(IMAGE_ALLOWED):
 allowed_asset_types.append(IMAGE_ALLOWED[key])

print("Currently allowed asset types: ")
for i in allowed_asset_types:
 print(i)

## Alien Entities
alien_limit = 67
alien_table = [[] for _ in range(alien_limit)]

## Define search patterns
pattern = SEARCHPARAMS

## Entity: In-transit Folders (ITFs)
data_dir = PATH_INSTANCE+DIRDATA
file_entity_manifest = data_dir+ENTITYMANIFEST
file_hash = data_dir+HASHNODE

## Slug1: Define your operation paths
thread_directory = str(THREADMEDIADIR)
thread_dehash = str(THREADDEHASH)
dir_photo_search = str(DIRPHOTO)

## Assembler: slug1,slug2,...,slugn
thread_photo = thread_directory+thread_dehash+dir_photo_search

## Static export operators
p = Path(thread_photo)
## p1 = Path(slug)

## BUCKET_OPERATOR_INSTANCE_HASHES = []
BUCKET_OPERATOR_INSTANCE_HASHES = []

class Slug:
 """
 Hold the 'HASHED_SLUG' in a 'hollow_class' and repopulate it later in the
 program.
 """
 HASHED_SLUG = SLUGHASH

## Operator OverClass
class Operator:
 ## Sanitize the function 'injection' parameters
 def provide_instance_hashes(type=""):
  """
  provide_instance_hashes is provided a filetype="" variable that can be
  overloaded to automate the instance hashes for various file types
  """
  BUCKET_HASHED_SLUGS.clear()
  BUCKET_OPERATOR_INSTANCE_HASHES.clear()
  if type=="":
   ## Init input setup: First Active Element (FAE) interaction model
   ext_get = input("Enter the extension type that you want to index: ")
  else:
   ext_get = str(type)

  inject_sanitized = pattern+ext_get
  ## 'ext' always takes a 'inject_sanitized' string as a desired type
  ## in this case, the desired type is 'str'
  ext = str(inject_sanitized)

  ## Create a key-pairing for the instance hashes
  z = {key: value for (key, value) in enumerate(p.glob(ext))}
  ## Append the dictionary instance
  BUCKET_OPERATOR_INSTANCE_HASHES.append(z)

  ## Printed Version
  #for i in enumerate(BUCKET_OPERATOR_INSTANCE_HASHES.values()):
  # print(i)

  if ext_get not in allowed_asset_types:
   print("Please provide an allowed asset type")
   print("Allowed Asset Types: ")
   for i in allowed_asset_types:
    print(i)
  else:
   with open(file_entity_manifest, oFo.write) as f:
    for key, value in BUCKET_OPERATOR_INSTANCE_HASHES[0].items():
     BUCKET_HASHED_SLUGS.append(PATH_INSTANCE+str(value))

    ## Sort the bucket
    BUCKET_HASHED_SLUGS.sort()

    for i in BUCKET_HASHED_SLUGS:
     f.write(i)

   f.close()

   ### RunTest: Display items from 'dir_photo_search'
   print('----- Double-check Test: -----')
   print('----- 1st Item -----')
   try:
    print(BUCKET_HASHED_SLUGS[0],'\n')
    print('----- 100th Item -----')
    try:
     print(BUCKET_HASHED_SLUGS[99],'\n')
     print('----- 1000th Item -----')
     try:
      print(BUCKET_HASHED_SLUGS[999],'\n')
     except IndexError:
      print("---- 1000th Item wasn't found ----")
      print("---- last known item is ----")
      print("Item: "+str(len(logi_bin))+": "+str(BUCKET_HASHED_SLUGS[-1]))
    except IndexError:
     print("--- 100th Item wasn't found ---")
     print("--- last known item is ----")
     print("Item: "+str(len(BUCKET_HASHED_SLUGS))+": "+\
      str(BUCKET_HASHED_SLUGS[-1]))
   except IndexError:
    print("---- No instances were hashed -----")

def provide_instance_hashes(type=""):
 """
 Generate an entity manifest of various types, or overload the instance
 with Operator.provide_instance_hashes(type="jpg")
 """
 Operator.provide_instance_hashes(type=type)

## Operations
## Path logic
def provide_instance_hash(instance=""):
 """
 provide_instance_hash takes the instance="" default but can be overloaded
 to automate the lookup
 core_hash.provide_instance_hash(
  instance="/NOVASTORE/PROGRAMS/trine/IDENTITY/PHOTO/media/background_demo.jpg")
 """
 if instance=="":
  filename_hash = input("Enter the file you need a hash for: ")
 else:
  filename_hash = instance
 try:
  with open(filename_hash, oFo.read_binary) as h:
   bytes = h.read()
   hash_value_md5 = md5(bytes).hexdigest()
   hash_value_sha1 = sha1(bytes).hexdigest()
   hash_value_sha256 = sha256(bytes).hexdigest()
   hash_value_sha512= sha512(bytes).hexdigest()
   ## Summarize the provided hash-schema
   print('MD5: ',hash_value_md5)
   print('SHA1: ',hash_value_sha1)
   print('SHA256: ',hash_value_sha256)
   print('SHA512: ',hash_value_sha512)
   md5_output = ('MD5: ', hash_value_md5)
   sha1_output = ('SHA1: ', hash_value_sha1)
   sha256_output = ('SHA256: ', hash_value_sha256)
   sha512_output = ('SHA512: ', hash_value_sha512)
   o = open(file_hash, oFo.write)
   output = 'File Inspector: '+\
   filename_hash, md5_output, sha1_output, sha256_output, sha512_output
   o.write(str(output))
   o.close()
   with open (file_hash, oFo.read_binary) as reader:
    Slug.HASHED_SLUG = reader.read()
    print('\n\n----- HASHED CONTENT -----\n\n')
    print(Slug.HASHED_SLUG)
   reader.close()
  h.close()
 except FileNotFoundError:
  print('File not found, try again.')

class AlienCoverUp:
 """
 Wipe the alien hash. reestablish the universe
 """
 def activated():
  #*********************************************#
  # START: STEP 3 VIRTUAL PROCESSOR             #
  #*********************************************#
  DEBUG_STEPS.step_3_virtual_processor(MODE="START")
  alien_table.clear()
  AlienCoverUp.setup_universe()
  ## Verify the default hash
  provide_instance_hash(instance=\
   "/NOVASTORE/PROGRAMS/trine/IDENTITY/PHOTO/media/background_demo.jpg")
  DEBUG_STEPS.step_3_virtual_processor(MODE="STOP")
  #********************************************#
  # STOP: STEP 3 VIRTUAL PROCESSOR             #
  #********************************************#

 def setup_universe():
  print(alien_table)
  base_function_alien = alien_limit - 1
  print(base_function_alien)

class AlienHash:
 """
 Usage: Provide a default alien hash run

 ## Get an nth value alien_hash
 AlienHash.alien_hash(x=30020)

 ## get_hash from the defined alien matrix table
 AlienHash.get_hash(given_value=30)
 """
 def alien_hash(x)-> int:
  """
  Return the 'alien_hash' value, providing the insert_key
  """
  return(round(float(x/4+20-1)))

 def get_hash(given_value=0)-> int:
  """
  If no value is given, the default value will be 0 and run
  the default 'random_limit' function
  """
  if given_value==0:
   random_limit = randint(1, alien_limit)
   x = random_limit
   return(AlienHash.alien_hash(x))
  else:
   x = given_value
   return(AlienHash.alien_hash(x))

 class Failure:
  def failed_value():
   pre_seed = round(alien_limit / randint(1, alien_limit))
   secure_alien_seed = pre_seed+randint(0, alien_limit-pre_seed)
   return(secure_alien_seed)

class AlienHashInsert:
 """
 'AlienHashInsert' is a prototype for inserting an Alien into a matrix
 according to a random alien hash.

 This random hash can help keep a value unspecified at runtime, such as a
 startup key slot, or a media asset that is being verified.

 You can then call upon the unique hashes when you need them, or clear the
 alien_table with an AlienCoverUp

 You can replace Failure.def_value() and AlienHash.alien_hash(x)
 with different extending functions to store temporary matrix values

 Alien hash: The Game Example
 Find the alien this way...
 """
 class TruthSeeker:
  """
  player_id must be in range of matrix blocks
  """
  seeker_id = 0
  AliasName = 'The Hidden, but True Alien'
  Name = 'John Smith'
  Skill = 71092

 ## Active Game Spaces in matrix format
 def insert(alien_table, key, values):
  try:
   alien_table[AlienHash.get_hash(given_value=key)].append(values)
  except IndexError:
   print("Index error starting backup.")
   key = AlienHash.Failure.failed_value()
   print("New Alien Identified: "+str(key))
   alien_table[AlienHash.get_hash(given_value=key)].append(values)

 def delete(alien_table, key, values):
  alien_table[AlienHash.get_hash(given_value=key)].remove(values)

## Default runner
if __name__ == MODULE_MATCHER_MAIN:
 setup_universe()



Return HOME