【问题标题】:Flutter grpc credentials using .pem file使用 .pem 文件颤振 grpc 凭据
【发布时间】:2020-04-29 11:16:17
【问题描述】:

我有一个 Flutter 应用,它需要通过 grpc 使用安全客户端通道与服务器通信。

为了尝试创建客户端频道,我正在使用:

 final _channel = ClientChannel(_hostAddress,
      port: _port,
      options:
          const ChannelOptions(credentials: ChannelCredentials.secure(
            certificates: ??? ???
          )));

对于certificates 部分,我唯一拥有的是 .pem 文件中的证书。

puv.dev 页面 here 上的文档完全没用,因为它只是指出证书参数是 int 列表

如何从 .pem 文件转到 int 列表

【问题讨论】:

    标签: flutter dart certificate grpc


    【解决方案1】:

    由于 Dart 没有字节类型,所以字节数组是 List 或 Uint8List。我想如果您打开 .pem 文件并将文件内容作为字节读取,您将获得 int 列表

    Future<Uint8List> readCert() async {
      final File f = File('cert.pem');
      final bytes = await f.readAsBytes();
      return bytes;
    }
    

    【讨论】:

      【解决方案2】:

      即使 @alex_z 的答案对于其他情况可能是正确的,但看起来正确的方法(如果证书文件在某处是资产,就像我的情况一样)是执行以下操作:

      final cert = await rootBundle.load('assets/certificate.pem');
      
      final certAsList = cert.buffer
              .asUint8List(cert.offsetInBytes, cert.lengthInBytes)
              .map((uint8) => uint8.toInt())
              .toList();
      

      这样,可以毫无问题地传递给ChannelCredentials

      【讨论】:

        猜你喜欢
        • 2021-07-04
        • 2020-04-24
        • 2021-03-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-01-27
        • 2023-03-30
        相关资源
        最近更新 更多