[Python]

Jae Gyeong Lee·2024년 11월 1일

hashlib, 암호화 관련

import hashlib
def encryption_code(codes):
    
    encryption_dic = {} #빈 dictionary 생성
    
    for code in codes:
        # hashlib.sha256() = code 값 SHA-256 해시로 변환
            # SHA-256 해시 알고리즘은 주어진 문자열을 고유한 해시 값으로 변환
                # SHA-256 해시 알고리즘은 '바이트' 형태의 데이터를 받음
                    #하여 .encode() 를 통해 code 변수를 바이트로 변환
        hash_object = hashlib.sha256(code.encode()) 
        
        #hash_object.hexdigest()를 사용하여 해시 값을 16진수 문자열로 변환
            # [:8] 를 사용해 처음 8자리만 가져오게 함
        random_str = f'S_{hash_object.hexdigest()[:8]}'
        
        # 변환된 문자열 dictionary에 저장
        encryption_dic[code] = random_str
        
        return encryption_dic
df = pd.read_excel(r'real_data_set.xlsx',
                   dtype=str)

unique_id = df['customer_id'].unique()
encryption_dic = encryption_code(unique_id)

# customer_id 값이 매핑 encryption_dic 에 있으면 해당 값으로 대체, 없으면 None
# map 함수의 인자로 dict data를 전달하면, 각 요소가 dict의 key로 사용되며, 매핑된 값을 반환
df['customer_id_encryption'] = df['r_cust_id'].map(encryption_dic)
profile
안녕하세요 반갑습니다. 공부한 내용들을 기록하고 있습니다.

0개의 댓글