Archived
1
0
Fork 0
This repository has been archived on 2024-04-26. You can view files and clone it, but cannot push or open issues or pull requests.
akari-bot/modules/hash/__init__.py

25 lines
1,019 B
Python
Raw Normal View History

2023-04-30 11:14:19 +00:00
import hashlib
from core.builtins import Bot
from core.component import module
2023-05-02 14:53:51 +00:00
h = module('hash', developers=['Dianliang233'], desc='{hash.help}', )
2023-04-30 11:14:19 +00:00
2023-05-02 14:53:51 +00:00
@h.handle('<algorithm> <str> [<encoding>] {{hash.help}}')
2023-04-30 11:14:19 +00:00
async def _(msg: Bot.MessageSession):
algorithm = msg.parsed_msg['<algorithm>']
string = msg.parsed_msg['<str>']
encoding = msg.parsed_msg.get('<encoding>', 'utf-8')
2023-04-30 11:27:53 +00:00
try:
hash_ = hashlib.new(algorithm, string.encode(encoding))
except ValueError:
2023-05-02 14:53:51 +00:00
await msg.finish(f"{msg.locale.t('hash.message.unsupported_algorithm', algorithm=algorithm)}\n"
f"{msg.locale.t('hash.message.algorithms_list', algorithms=', '.join(hashlib.algorithms_available))}")
await msg.finish(msg.locale.t('hash.message.output', algorithm=hash_.name, digest=hash_.hexdigest()))
2023-04-30 11:14:19 +00:00
2023-05-02 14:53:51 +00:00
@h.handle('list {{hash.help.list}}')
2023-04-30 11:14:19 +00:00
async def _(msg: Bot.MessageSession):
2023-05-02 14:53:51 +00:00
await msg.finish(msg.locale.t('hash.message.algorithms_list', algorithms=', '.join(hashlib.algorithms_available)))