Code Monkey home page Code Monkey logo

Comments (1)

rohintoncollins avatar rohintoncollins commented on May 23, 2024

I came up with a solution to this by using the elliptic package in conjunction with the web crypto package. I used the elliptic package to create a PrivateKey using the 32 bytes of the private key ('d'). I then generated the PublicKey from the PrivateKey and converted both keys to PKCS#8 format. I was then able to use the PKCS#8 constructor in web crypto to create an EcdsaPrivateKey which I could then use for ECDSA (sign). I hard-coded the PKCS#8 format, as I am only using a single curve (NIST P256 / secp256r1 / prime256v1). Here is my code in case it is useful for anyone who finds this issue. The integerListFromHexString is external to this code. You could use the hex package instead.

import 'dart:typed_data';
import 'package:elliptic/elliptic.dart' hide EllipticCurve;
import 'package:webcrypto/webcrypto.dart';
import 'package:zcrypto/src/convert.dart';

/// ECDSA sign/verify using NIST-P256 algorithm
class Ecdsa {
  /// We need to convert elliptic.PrivateKey and elliptic.PublicKey -> webcrypto.EcdsaPrivateKey
  static Future<Uint8List> sign({required Uint8List message, required PrivateKey privateKey}) async {
    final pkcs8 = await
      '308187020100301306072a8648ce3d020106082a8648ce3d030107046d306b0201010420'
      '${privateKey.toHex()}'
      'a144034200'
      '${privateKey.publicKey.toHex()}'.integerListFromHexString();
    final signingKey = await EcdsaPrivateKey.importPkcs8Key(pkcs8, EllipticCurve.p256);
    return await signingKey.signBytes(message, Hash.sha256);
  }
  
  /// For verification we can convert directly from elliptic.PublicKey -> webcrypto.EcdsaPublicKey
  static Future<bool> verify({required Uint8List message, required Uint8List signature, required PublicKey publicKey}) async {    
    final publicKeyBytes = await publicKey.toHex().integerListFromHexString();
    final verifyingKey = await EcdsaPublicKey.importRawKey(publicKeyBytes, EllipticCurve.p256);
    return await verifyingKey.verifyBytes(signature, message, Hash.sha256);
  }
}

from webcrypto.dart.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.