我正在尝试在Python中使用Chromium cookie,因为Chromium使用AES(使用CBC)对其cookie进行加密,我需要对此进行反转.
我可以从OS X的Keychain中恢复AES密钥(它存储在Base 64中):
security find-generic-password -w -a Chrome -s Chrome Safe Storage
# From Python:
python -c 'from subprocess import PIPE, Popen; print(Popen(['security', 'find-generic-password', '-w', '-a', 'Chrome', '-s', 'Chrome Safe Storage'], stdout=PIPE).stdout.read().strip())'
Run Code Online (Sandbox Code Playgroud)
这是我的代码,我所缺少的是解密cookie:
from subprocess import PIPE, Popen
from sqlite3 import dbapi2
def get_encryption_key():
cmd = ['security', 'find-generic-password', '-w', '-a', 'Chrome', '-s', 'Chrome Safe Storage']
return Popen(cmd, stdout=PIPE).stdout.read().strip().decode('base-64')
def get_cookies(database):
key = get_encryption_key()
with dbapi2.connect(database) as conn:
conn.rollback()
rows = conn.cursor().execute('SELECT name, encrypted_value FROM cookies WHERE host_key like …Run Code Online (Sandbox Code Playgroud) 我想强制关联类型为Self,但编译器没有。
这是我想要编译的内容:
protocol Protocol {
// Error: Inheritance from non-protocol, non-class type 'Self'
associatedtype Type: Self
}
Run Code Online (Sandbox Code Playgroud)
你可能会问,为什么不直接使用Self而不是关联类型呢?仅仅因为我不能:关联类型是从父协议继承的。在父协议中更改它没有意义。
这是类似于我正在尝试做的事情:
protocol Factory {
associatedtype Type
func new() -> Type
}
protocol SelfFactory: Factory {
associatedtype Type: Self // Same Error
}
Run Code Online (Sandbox Code Playgroud)
编辑:
马特的答案几乎就是我要找的。它的行为就像我希望它在运行时一样,但在编译时不够严格。
我希望这是不可能的:
protocol Factory {
associatedtype MyType
static func new() -> MyType
}
protocol SelfFactory: Factory {
static func new() -> Self
}
final class Class: SelfFactory {
// Implement SelfFactory:
static func new() -> …Run Code Online (Sandbox Code Playgroud)