import { sm3 } from '@ont-dev/sm.js' ; import * as cryptoJS from 'crypto-js' ; import { sha3_224 , sha3_256 , sha3_384 , sha3_512 } from 'js-sha3' ; import { DEFAULT_ALGORITHM } from '../consts' ; import { hexstring2ab } from '../utils' ; import { CurveLabel } from './CurveLabel' ; import { KeyType } from './KeyType' ; import { SignatureScheme } from './SignatureScheme' ; /**
 * Specific parameters for the key type.
 */ export class KeyParameters { param json static deserializeJson ( json ) { return new KeyParameters ( CurveLabel . fromLabel ( json . curve ) ) ; } curve : CurveLabel ; constructor ( curve ) { this . curve = curve ; } /**
     * Serialize KeyParameters to json.
     */ serializeJson ( ) { return { curve : this . curve . label } ; } } /**
 * Common representation of private or public key
 */ export class Key { /**
     * Algorithm used for key generation.
     */ algorithm : KeyType ; /**
     * Parameters of the algorithm.
     */ parameters : KeyParameters ; /**
     * Key data.
     */ key : string ; param key param algorithm param parameters constructor ( key , algorithm ? , parameters ? ) { this . key = key ; if ( algorithm === undefined ) { algorithm = KeyType . fromLabel ( DEFAULT_ALGORITHM . algorithm ) ; } if ( parameters === undefined ) { parameters = KeyParameters . deserializeJson ( DEFAULT_ALGORITHM . parameters ) ; } this . algorithm = algorithm ; this . parameters = parameters ; } param msg param scheme computeHash ( msg , scheme ) { switch ( scheme ) { case SignatureScheme . ECDSAwithSHA224 : return cryptoJS . SHA224 ( cryptoJS . enc . Hex . parse ( msg ) ) . toString ( ) ; case SignatureScheme . ECDSAwithSHA256 : return cryptoJS . SHA256 ( cryptoJS . enc . Hex . parse ( msg ) ) . toString ( ) ; case SignatureScheme . ECDSAwithSHA384 : return cryptoJS . SHA384 ( cryptoJS . enc . Hex . parse ( msg ) ) . toString ( ) ; case SignatureScheme . ECDSAwithSHA512 : case SignatureScheme . EDDSAwithSHA512 : return cryptoJS . SHA512 ( cryptoJS . enc . Hex . parse ( msg ) ) . toString ( ) ; case SignatureScheme . ECDSAwithSHA3_224 : return sha3_224 ( hexstring2ab ( msg ) ) ; case SignatureScheme . ECDSAwithSHA3_256 : return sha3_256 ( hexstring2ab ( msg ) ) ; case SignatureScheme . ECDSAwithSHA3_384 : return sha3_384 ( hexstring2ab ( msg ) ) ; case SignatureScheme . ECDSAwithSHA3_512 : return sha3_512 ( hexstring2ab ( msg ) ) ; case SignatureScheme . ECDSAwithRIPEMD160 : return cryptoJS . RIPEMD160 ( cryptoJS . enc . Hex . parse ( msg ) ) . toString ( ) ; case SignatureScheme . SM2withSM3 : return ( new sm3 ( ) ) . sum ( hexstring2ab ( msg ) , 'hex' ) ; default : throw new Error ( 'Unsupported hash algorithm.' ) ; } } param schema isSchemaSupported ( schema ) { switch ( schema ) { case SignatureScheme . ECDSAwithSHA224 : case SignatureScheme . ECDSAwithSHA256 : case SignatureScheme . ECDSAwithSHA384 : case SignatureScheme . ECDSAwithSHA512 : case SignatureScheme . ECDSAwithSHA3_224 : case SignatureScheme . ECDSAwithSHA3_256 : case SignatureScheme . ECDSAwithSHA3_384 : case SignatureScheme . ECDSAwithSHA3_512 : case SignatureScheme . ECDSAwithRIPEMD160 : return this . algorithm === KeyType . ECDSA ; case SignatureScheme . EDDSAwithSHA512 : return this . algorithm === KeyType . EDDSA ; case SignatureScheme . SM2withSM3 : return this . algorithm === KeyType . SM2 ; default : throw new Error ( 'Unsupported signature schema.' ) ; } } /**
     * Gets JSON representation of the Key (Public/Private).
     */ serializeJson ( ) { return { algorithm : this . algorithm . label , parameters : this . parameters . serializeJson ( ) , key : this . key } ; } } /**
 * Json representation of the Key.
 */ export interface JsonKey { algorithm : string ; parameters : JsonKeyParameters ; key : string | null ; external ? : any | null ; } /**
 * Json representation of the Key parameters.
 */ export interface JsonKeyParameters { curve : string ; }