Net.Pkcs11Interop.HighLevelAPI81.Session.CreateObject C# (CSharp) Method

CreateObject() public method

Creates a new object
public CreateObject ( List attributes ) : ObjectHandle
attributes List Object attributes
return ObjectHandle
        public ObjectHandle CreateObject(List<ObjectAttribute> attributes)
        {
            if (this._disposed)
                throw new ObjectDisposedException(this.GetType().FullName);

            ulong objectId = CK.CK_INVALID_HANDLE;

            CK_ATTRIBUTE[] template = null;
            ulong templateLength = 0;
            
            if (attributes != null)
            {
                templateLength = Convert.ToUInt64(attributes.Count);
                template = new CK_ATTRIBUTE[templateLength];
                for (int i = 0; i < Convert.ToInt32(templateLength); i++)
                    template[i] = attributes[i].CkAttribute;
            }

            CKR rv = _p11.C_CreateObject(_sessionId, template, templateLength, ref objectId);
            if (rv != CKR.CKR_OK)
                throw new Pkcs11Exception("C_CreateObject", rv);

            return new ObjectHandle(objectId);
        }

Usage Example

Example #1
0
 /// <summary>
 /// Creates the data object.
 /// </summary>
 /// <param name='session'>Read-write session with user logged in</param>
 /// <returns>Object handle</returns>
 public static ObjectHandle CreateDataObject(Session session)
 {
     // Prepare attribute template of new data object
     List<ObjectAttribute> objectAttributes = new List<ObjectAttribute>();
     objectAttributes.Add(new ObjectAttribute(CKA.CKA_CLASS, CKO.CKO_DATA));
     objectAttributes.Add(new ObjectAttribute(CKA.CKA_TOKEN, true));
     objectAttributes.Add(new ObjectAttribute(CKA.CKA_APPLICATION, Settings.ApplicationName));
     objectAttributes.Add(new ObjectAttribute(CKA.CKA_LABEL, Settings.ApplicationName));
     objectAttributes.Add(new ObjectAttribute(CKA.CKA_VALUE, "Data object content"));
     
     // Create object
     return session.CreateObject(objectAttributes);
 }