How to use the fabric-client/lib/utils.js.getCryptoSuite function in fabric-client

To help you get started, we’ve selected a few fabric-client examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github hyperledger / fabric-sdk-node / test / unit / headless-tests.js View on Github external
test('\n\n** utils.getCryptoSuite tests **\n\n', (t) => {
	var cs = utils.getCryptoSuite({keysize: 384, algorithm: 'EC'}, keyValStorePath5);
	t.equal(cs instanceof CryptoSuite_ECDSA_AES, true, 'Should return an instance of CryptoSuite_ECDSA_AES');
	t.equal(cs._keySize, 384, 'Returned instance should have keysize of 384');
	t.equal(cs._storePath, keyValStorePath5, 'Returned instance should have store path of ' + keyValStorePath5);

	cs = utils.getCryptoSuite({keysize: 384, algorithm: 'EC'}, keyValStorePath5);
	t.equal(cs instanceof CryptoSuite_ECDSA_AES, true, 'Default test: should return an instance of CryptoSuite_ECDSA_AES');
	t.equal(cs._keySize, 384, 'Returned instance should have keysize of 384');
	t.equal(cs._storePath, keyValStorePath5, 'Returned instance should have store path of ' + keyValStorePath5);

	cs = utils.getCryptoSuite({algorithm: 'EC'}, keyValStorePath5);
	t.equal(cs instanceof CryptoSuite_ECDSA_AES, true, 'Should return an instance of CryptoSuite_ECDSA_AES');
	t.equal(cs._keySize, 256, 'Returned instance should have keysize of 256');
	t.equal(cs._storePath, keyValStorePath5, 'Returned instance should have store path of ' + keyValStorePath5);

	// each app instance is expected to use either HSM or software-based key management, as such this question
	// is answered with a config setting rather than controlled on a case-by-case basis
github hyperledger / fabric-sdk-node / test / unit / headless-tests.js View on Github external
t.equal(cryptoUtils.hash(TEST_MSG), HASH_MSG_SHA256,
		'CryptoSuite_ECDSA_AES function tests: using "SHA2" hashing algorithm with default key size which should be 256');

	t.equal(cryptoUtils.hash(TEST_LONG_MSG), HASH_LONG_MSG_SHA256,
		'CryptoSuite_ECDSA_AES function tests: using "SHA2" hashing algorithm with default key size which should be 256');

	// test SHA384 hash
	utils.setConfigSetting('crypto-hash-algo', 'sha2');
	utils.setConfigSetting('crypto-keysize', 384);
	cryptoUtils = utils.getCryptoSuite();
	t.equal(cryptoUtils.hash(TEST_MSG), HASH_MSG_SHA384,
		'CryptoSuite_ECDSA_AES function tests: using "SHA2" hashing algorithm with default key size which should be 384');

    //reset to default key size
	utils.setConfigSetting('crypto-keysize', 256);
	cryptoUtils = utils.getCryptoSuite();

	cryptoUtils.generateKey()
		.then(function (key) {
			t.equal('secp256r1', key.getPublicKey()._key.curveName,
				'CryptoSuite_ECDSA_AES function tests: cryptoUtils generated public key curveName == secp256r1');

			// test curve 256 with SHA3_256
			utils.setConfigSetting('crypto-hash-algo', 'SHA3');
			utils.setConfigSetting('crypto-keysize', 256);
			cryptoUtils = utils.getCryptoSuite();
			return cryptoUtils.generateKey();
		})
		.then(function (key) {
			t.equal('secp256r1', key.getPublicKey()._key.curveName,
				'CryptoSuite_ECDSA_AES function tests: ccryptoUtils generated public key curveName == secp256r1');
github hyperledger / fabric-sdk-node / test / unit / headless-tests.js View on Github external
t.throws(
		function() {
			new SigningIdentity('id', 'cert', 'pubKey', 'msp');
		},
		/Missing required parameter "signer"/,
		'Checking required input parameters'
	);

	// test identity serialization and deserialization
	var mspImpl = new MSP({
		trustedCerts: [],
		signer: 'blah',
		admins: [],
		id: 'testMSP',
		cryptoSuite: utils.getCryptoSuite()
	});

	var cryptoUtils = utils.getCryptoSuite();
	var pubKey = cryptoUtils.importKey(TEST_CERT_PEM, { algorithm: api.CryptoAlgorithms.X509Certificate });
	var identity = new Identity('testIdentity', TEST_CERT_PEM, pubKey, mspImpl);

	var serializedID = identity.serialize();
	mspImpl.deserializeIdentity(serializedID)
	.then((dsID) => {
		t.equal(dsID._certificate, TEST_CERT_PEM, 'Identity class function tests: deserialized certificate');
		t.equal(dsID._publicKey.isPrivate(), false, 'Identity class function tests: deserialized public key');
		t.equal(dsID._publicKey._key.pubKeyHex, '0452a75e1ee105da7ab3d389fda69d8a04f5cf65b305b49cec7cdbdeb91a585cf87bef5a96aa9683d96bbabfe60d8cc6f5db9d0bc8c58d56bb28887ed81c6005ac', 'Identity class function tests: deserialized public key ecparam check');

		// manually construct a key based on the saved privKeyHex and pubKeyHex
		var f = KEYUTIL.getKey(TEST_KEY_PRIVATE_PEM);
		var testKey = new ecdsaKey(f);
github hyperledger / fabric-sdk-node / test / unit / headless-tests.js View on Github external
new SigningIdentity('id', 'cert', 'pubKey', 'msp');
		},
		/Missing required parameter "signer"/,
		'Checking required input parameters'
	);

	// test identity serialization and deserialization
	var mspImpl = new MSP({
		trustedCerts: [],
		signer: 'blah',
		admins: [],
		id: 'testMSP',
		cryptoSuite: utils.getCryptoSuite()
	});

	var cryptoUtils = utils.getCryptoSuite();
	var pubKey = cryptoUtils.importKey(TEST_CERT_PEM, { algorithm: api.CryptoAlgorithms.X509Certificate });
	var identity = new Identity('testIdentity', TEST_CERT_PEM, pubKey, mspImpl);

	var serializedID = identity.serialize();
	mspImpl.deserializeIdentity(serializedID)
	.then((dsID) => {
		t.equal(dsID._certificate, TEST_CERT_PEM, 'Identity class function tests: deserialized certificate');
		t.equal(dsID._publicKey.isPrivate(), false, 'Identity class function tests: deserialized public key');
		t.equal(dsID._publicKey._key.pubKeyHex, '0452a75e1ee105da7ab3d389fda69d8a04f5cf65b305b49cec7cdbdeb91a585cf87bef5a96aa9683d96bbabfe60d8cc6f5db9d0bc8c58d56bb28887ed81c6005ac', 'Identity class function tests: deserialized public key ecparam check');

		// manually construct a key based on the saved privKeyHex and pubKeyHex
		var f = KEYUTIL.getKey(TEST_KEY_PRIVATE_PEM);
		var testKey = new ecdsaKey(f);
		var pubKey = testKey.getPublicKey();

		var signer = new Signer(cryptoUtils, testKey);
github hyperledger / fabric-sdk-node / test / unit / headless-tests.js View on Github external
var member2 = new User(memberCfg, _client);
	if (member2.getName() === enrollmentID)
		t.pass('User constructor test 2: new User cfg getName was successful');
	else
		t.fail('User constructor test 2: new User cfg getName was not successful');

	if (member2.getRoles() &&
		member2.getRoles().indexOf('admin') > -1 &&
		member2.getRoles().indexOf('user') > -1)
		t.pass('User constructor test 2: new User cfg getRoles was successful');
	else
		t.fail('User constructor test 2: new User cfg getRoles was not successful');

	// test set enrollment for identity and signing identity
	var cryptoUtils = utils.getCryptoSuite();
	cryptoUtils.generateKey()
	.then(function (key) {
		// the private key and cert don't match, but it's ok, the code doesn't check
		member2.setEnrollment(key, TEST_CERT_PEM)
		.then(() => {
			var id = member2.getIdentity();

			t.equal(id._publicKey._key.pubKeyHex, '0452a75e1ee105da7ab3d389fda69d8a04f5cf65b305b49cec7cdbdeb91a585cf87bef5a96aa9683d96bbabfe60d8cc6f5db9d0bc8c58d56bb28887ed81c6005ac', 'User class setEnrollment() test');
			t.end();
		});

		// TODO: test SigningIdentity
		t.end();
	});

	t.end();
github hyperledger / fabric-sdk-node / test / unit / headless-tests.js View on Github external
.then(function (key) {
			t.equal('secp256r1', key.getPublicKey()._key.curveName,
				'CryptoSuite_ECDSA_AES function tests: ccryptoUtils generated public key curveName == secp256r1');

			t.equal(cryptoUtils.hash(TEST_MSG), HASH_MSG_SHA3_256,
				'CryptoSuite_ECDSA_AES function tests: using "SHA3" hashing algorithm with key size 256');

			t.equal(cryptoUtils.hash(TEST_LONG_MSG), HASH_LONG_MSG_SHA3_256,
				'CryptoSuite_ECDSA_AES function tests: using "SHA3" hashing algorithm with key size 256');

			// test SHA3_384
			utils.setConfigSetting('crypto-keysize', 384);
			cryptoUtils = utils.getCryptoSuite();

			t.equal(cryptoUtils.hash(TEST_MSG), HASH_MSG_SHA3_384,
				'CryptoSuite_ECDSA_AES function tests: using "SHA3" hashing algorithm with key size 384');

			t.equal(cryptoUtils.hash(TEST_LONG_MSG), HASH_LONG_MSG_SHA3_384,
				'CryptoSuite_ECDSA_AES function tests: using "SHA3" hashing algorithm with key size 384');

			return cryptoUtils.generateKey();
		})
		.then(function (key) {
github hyperledger / fabric-sdk-node / test / unit / headless-tests.js View on Github external
},
				/A valid signature is required to verify/,
				'CryptoSuite_ECDSA_AES function tests: verify() should throw "A valid signature is required to verify"'
			);

			t.throws(
				function () {
					cryptoUtils.verify('dummy key', 'dummy signature');
				},
				/A valid message is required to verify/,
				'CryptoSuite_ECDSA_AES function tests: verify() should throw "A valid message is required to verify"'
			);

			utils.setConfigSetting('crypto-keysize', 256);
			utils.setConfigSetting('crypto-hash-algo', 'SHA2');
			cryptoUtils = utils.getCryptoSuite();

			var testVerify = function (sig, msg, expected) {
				// manually construct a key based on the saved privKeyHex and pubKeyHex
				var f = new ECDSA({ curve: 'secp256r1' });
				f.setPrivateKeyHex(TEST_KEY_PRIVATE);
				f.setPublicKeyHex(TEST_KEY_PUBLIC);
				f.isPrivate = true;
				f.isPublic = false;

				t.equal(cryptoUtils.verify(new ecdsaKey(f), sig, msg), expected,
					'CryptoSuite_ECDSA_AES function tests: verify() method');
			};

			// these signatures have S values larger than N/2
			testVerify(TEST_MSG_SIGNATURE_SHA2_256, TEST_MSG, false);
			testVerify(TEST_LONG_MSG_SIGNATURE_SHA2_256, TEST_LONG_MSG, false);
github hyperledger / fabric-sdk-node / test / unit / headless-tests.js View on Github external
test('\n\n ** lib/Client.js **\n\n', function (t) {

	t.equals(client.getCryptoSuite(), null, 'Client getCryptoSuite should initially be null');
	client.setCryptoSuite(utils.getCryptoSuite());
	if (client.getCryptoSuite() != null) t.pass('Client getCryptoSuite should not be null after setCryptoSuite');

	client.getUserContext()
	.then(function(response){
		if (response === null)
			t.pass('Client tests: getUserContext successful null user name.');
		else t.fail('Client tests: getUserContext failed null name check');
	}, function(error){
		t.fail('Client tests: Unexpected error, getUserContext null name check. ' + error);
	});

	client.saveUserToStateStore()
	.then(function(response){
		t.fail('Client tests: got response, but should throw "Cannot save user to state store when userContext is null."');
	}, function(error){
		if (error.message === 'Cannot save user to state store when userContext is null.')
github hyperledger / fabric-sdk-node / test / unit / headless-tests.js View on Github external
() => {
			cs = utils.getCryptoSuite({lib: '/usr/local/lib', slot: 0, pin: '1234' });
		},
		/^Error: The "kvsPath" parameter for this constructor, if specified, must be a string specifying a file system path/,
github hyperledger / fabric-sdk-node / test / unit / headless-tests.js View on Github external
'CryptoSuite_ECDSA_AES function tests: setting key size 123 should throw Illegal level error'
			);

			t.throws(
				function () {
					utils.setConfigSetting('crypto-keysize', 256);
					utils.setConfigSetting('crypto-hash-algo', '12345');
					cryptoUtils = utils.getCryptoSuite();
				},
				/^Error: Unsupported hash algorithm/,
				'CryptoSuite_ECDSA_AES function tests: setting hash algo to 12345 should throw Illegal Hash function family'
			);

			utils.setConfigSetting('crypto-keysize', 256);
			utils.setConfigSetting('crypto-hash-algo', 'SHA3');
			cryptoUtils = utils.getCryptoSuite();
			return cryptoUtils.generateKey();
		})
		.then(function (key) {