# Path: kinto-remote-settings/src/kinto_remote_settings/signer/utils.py
# def get_first_matching_setting(setting_name, settings, prefixes, default=None):
#     for prefix in prefixes:
#         prefixed_setting_name = prefix + setting_name
#         if prefixed_setting_name in settings:
#             return settings[prefixed_setting_name]
#     return default
# 
# Path: kinto-remote-settings/src/kinto_remote_settings/signer/backends/base.py
# class SignerBase(object):
#     def sign(self, payload):
#         """
#         Signs the specified `payload` and returns the signature metadata.
# 
#         :returns: A mapping with every attributes about the signature
#             (e.g. "signature", "hash_algorithm", "signature_encoding"...)
#         :rtype: dict
#         """
#         raise NotImplementedError
# 
# Path: kinto-remote-settings/src/kinto_remote_settings/signer/backends/exceptions.py
# class BadSignatureError(Exception):
#     pass


import base64
import hashlib
import warnings
import ecdsa
from ecdsa import NIST384p, SigningKey, VerifyingKey
from ..utils import get_first_matching_setting
from .base import SignerBase
from .exceptions import BadSignatureError




# Autograph uses this prefix prior to signing.
SIGN_PREFIX = b"Content-Signature:\x00"


class ECDSASigner(SignerBase):
    def __init__(self, private_key=None, public_key=None):
        if private_key is None and public_key is None:
            msg = "Please, specify either a private_key or public_key " "location."
            raise ValueError(msg)
        self.private_key = private_key
        self.public_key = public_key

    @classmethod
    def generate_keypair(cls):
        sk = SigningKey.generate(curve=NIST384p)
        vk = sk.get_verifying_key()
        return sk.to_pem(), vk.to_pem()

    def load_private_key(self):
        if self.private_key is None:
            msg = "Please, specify the private_key location."
            raise ValueError(msg)

        with open(self.private_key, "rb") as key_file:
            return SigningKey.from_pem(key_file.read())


# Path: kinto-remote-settings/src/kinto_remote_settings/signer/utils.py
# def get_first_matching_setting(setting_name, settings, prefixes, default=None):
#     for prefix in prefixes:
#         prefixed_setting_name = prefix + setting_name
#         if prefixed_setting_name in settings:
#             return settings[prefixed_setting_name]
#     return default
# 
# Path: kinto-remote-settings/src/kinto_remote_settings/signer/backends/base.py
# class SignerBase(object):
#     def sign(self, payload):
#         """
#         Signs the specified `payload` and returns the signature metadata.
# 
#         :returns: A mapping with every attributes about the signature
#             (e.g. "signature", "hash_algorithm", "signature_encoding"...)
#         :rtype: dict
#         """
#         raise NotImplementedError
# 
# Path: kinto-remote-settings/src/kinto_remote_settings/signer/backends/exceptions.py
# class BadSignatureError(Exception):
#     pass


# Path: kinto-remote-settings/src/kinto_remote_settings/signer/backends/local_ecdsa.py
import base64
import hashlib
import warnings
import ecdsa
from ecdsa import NIST384p, SigningKey, VerifyingKey
from ..utils import get_first_matching_setting
from .base import SignerBase
from .exceptions import BadSignatureError





# Autograph uses this prefix prior to signing.
SIGN_PREFIX = b"Content-Signature:\x00"


class ECDSASigner(SignerBase):
    def __init__(self, private_key=None, public_key=None):
        if private_key is None and public_key is None:
            msg = "Please, specify either a private_key or public_key " "location."
            raise ValueError(msg)
        self.private_key = private_key
        self.public_key = public_key

    @classmethod
    def generate_keypair(cls):
        sk = SigningKey.generate(curve=NIST384p)
        vk = sk.get_verifying_key()
        return sk.to_pem(), vk.to_pem()

    def load_private_key(self):
        if self.private_key is None:
            msg = "Please, specify the private_key location."
            raise ValueError(msg)

        with open(self.private_key, "rb") as key_file:
            return SigningKey.from_pem(key_file.read())


    def load_public_key(self):