Exemple #1
0
        public static unsafe byte[] SignText(string src, IPrivKey key)
        {
            const int max_length = 2048;
            sbyte *   tmp        = stackalloc sbyte[max_length];

            byte[] bsrc   = cp1251.GetBytes(src);
            byte[] result = new byte[max_length];

            int rc;

            fixed(byte *psrc = bsrc)
            rc = Crypt_Sign(psrc, bsrc.Length, tmp, max_length, key.getPKey());

            if (rc < 0)
            {
                throw (new IPrivException(rc));
            }
            // return new string(tmp, 0, rc, cp1251);
            int len = 0;

            for (int i = 0; i < max_length; i++)
            {
                len++;
                if (tmp[i] == 0)
                {
                    break;
                }
            }
            result = new byte[len];
            for (int i = 0; i < len; i++)
            {
                result[i] = (byte)tmp[i];
            }
            return(HttpUtility.UrlEncodeToBytes(result));
        }
Exemple #2
0
        public static unsafe string verifyText(string src, IPrivKey key)
        {
            byte[] srcb = cp1251.GetBytes(src);
            fixed(byte *psrc = srcb)
            {
                sbyte *pdst  = (sbyte *)IntPtr.Zero;
                int    pndst = 0;
                int    rc    = Crypt_Verify(psrc, srcb.Length, &pdst, ref pndst, key.getPKey());

                if (rc != 0)
                {
                    throw (new IPrivException(rc));
                }
                return(new string(pdst, 0, pndst, cp1251));
            }
        }
Exemple #3
0
        public static unsafe string signText(string src, IPrivKey key)
        {
            const int max_length = 2048;
            sbyte *   tmp        = stackalloc sbyte[max_length];

            byte[] bsrc = cp1251.GetBytes(src);
            int    rc;

            fixed(byte *psrc = bsrc)
            rc = Crypt_Sign(psrc, bsrc.Length, tmp, max_length, key.getPKey());

            if (rc < 0)
            {
                throw (new IPrivException(rc));
            }
            return(new string(tmp, 0, rc, cp1251));
        }
Exemple #4
0
        public static unsafe IPrivKey openPublicKey(string path, uint keyserial)
        {
            IPrivKey k = new IPrivKey();

            byte[] bpath = new byte[path.Length + 1];            //zero-terminated string
            cp1251.GetBytes(path, 0, path.Length, bpath, 0);
            fixed(byte *ppath = bpath)
            {
                int rc = Crypt_OpenPublicKeyFromFile(0, ppath, keyserial, k.getPKey(), null);

                if (rc != 0)
                {
                    throw (new IPrivException(rc));
                }
            }

            return(k);
        }
Exemple #5
0
        public static unsafe IPrivKey openSecretKey(string path, string passwd)
        {
            IPrivKey k = new IPrivKey();

            byte[] bpath   = new byte[path.Length + 1];          //zero-terminated string
            byte[] bpasswd = new byte[passwd.Length + 1];        //zero-terminated string
            cp1251.GetBytes(path, 0, path.Length, bpath, 0);
            cp1251.GetBytes(passwd, 0, passwd.Length, bpasswd, 0);
            fixed(byte *ppath = bpath)
            fixed(byte *ppasswd = bpasswd)
            {
                int rc = Crypt_OpenSecretKeyFromFile(0, ppath, ppasswd, k.getPKey());

                if (rc != 0)
                {
                    throw (new IPrivException(rc));
                }
            }
            return(k);
        }
Exemple #6
0
 public static unsafe void closeKey(IPrivKey key)
 {
     Crypt_CloseKey(key.getPKey());
 }