Push notification Erlang:将vapid keys PEM文件转换为基本64字符串格式(applicationServerKey)

Push notification Erlang:将vapid keys PEM文件转换为基本64字符串格式(applicationServerKey),push-notification,openssl,jwt,erlang,vapid,Push Notification,Openssl,Jwt,Erlang,Vapid,嗨,我想从erlang服务器应用程序发送FCM推送通知。 当用户在浏览器中订阅FCM通知时。swRegistration.pushManager.subscribe()需要公钥(applicationServerKey) 所以为了得到这个密钥,我使用openssl并生成了两个PEM文件-私钥和公钥 openssl ecparam -name prime256v1 -genkey -noout -out es_private_key.pem openssl ec -in es_private

嗨,我想从erlang服务器应用程序发送FCM推送通知。 当用户在浏览器中订阅FCM通知时。swRegistration.pushManager.subscribe()需要公钥(applicationServerKey)

所以为了得到这个密钥,我使用openssl并生成了两个PEM文件-私钥和公钥

openssl ecparam -name prime256v1 -genkey -noout -out es_private_key.pem   
openssl ec -in es_private_key.pem -pubout -out es_public_key.pem
我知道你们可以从firebase控制台获取私钥和公钥。但提供的钥匙不在 PEM格式和签署JWT的erlang库使用PEM文件进行签署和验证

{ok, PrivtPem} = file:read_file("path/to/es_private_key.pem"),
Jwt = jwerl:sign([{name, <<"bob">>}], es256, PrivtPem).

几天后,我发现了如何在Erlang中实现这一点

首先,我使用了一个叫做弗拉基米尔的图书馆

然后我使用了作者使用的代码,最后我对ECPoint bin内容进行base64Url编码

下面是erlang代码:

pem_to_string()->
{ok, key} = file:read_file("/home/keys/es_public_key.pem"),
 [SPKI] = public_key:pem_decode(Key),
  #'SubjectPublicKeyInfo'{algorithm = Der} = SPKI,
  RealSPKI = public_key:der_decode('SubjectPublicKeyInfo', Der),
  #'SubjectPublicKeyInfo'{
     subjectPublicKey = Octets,
     algorithm = #'AlgorithmIdentifier'{ parameters = Params}
    } = RealSPKI,
  ECPoint = #'ECPoint'{point = Octets}, 
{'ECPoint', Bin} = ECPoint,
base64url:encode(Bin).
我不是加密和曲线密钥方面的专家,我只想为推送内容签名,因为谷歌想要这个。这段代码给了我64个八位字节base64编码的公钥。我可以在推送FCM中使用此公钥

我希望这对某人有用

pem_to_string()->
{ok, key} = file:read_file("/home/keys/es_public_key.pem"),
 [SPKI] = public_key:pem_decode(Key),
  #'SubjectPublicKeyInfo'{algorithm = Der} = SPKI,
  RealSPKI = public_key:der_decode('SubjectPublicKeyInfo', Der),
  #'SubjectPublicKeyInfo'{
     subjectPublicKey = Octets,
     algorithm = #'AlgorithmIdentifier'{ parameters = Params}
    } = RealSPKI,
  ECPoint = #'ECPoint'{point = Octets}, 
{'ECPoint', Bin} = ECPoint,
base64url:encode(Bin).