Beispiel #1
0
        T DecryptFiveByFive(T item)
        {
            var properties = ToDictionary(new ProjectionProxy(item));
            var tokenId    = properties["id"];
            var code       = properties["code"];
            var hash       = properties["hash"];
            var salt       = TokenIdentifier.Parse(tokenId.Value).MintRequestId.ToByteArray();

            using (var tokenCode = tokenSecurity.DecryptFromBase64(code.Value, hash.Value, salt))
            {
                var fiveByFive = SecureStringToGlobalAllocAnsi(tokenCode);

                try
                {
                    code.Value = PtrToStringAnsi(fiveByFive);
                }
                finally
                {
                    if (fiveByFive != IntPtr.Zero)
                    {
                        ZeroFreeGlobalAllocAnsi(fiveByFive);
                    }
                }
            }

            return(item);
        }
        public async Task <TokenPackage> CreatePackage(IEnumerable <TokenReference> tokens, string thumbprint)
        {
            var certificate = certificateLocator.LocateByThumbprint(thumbprint);

            //var publicKey = certificate.PublicKey.Key;

            // TODO: encrypt package file stream using certificate
            // TODO: compress package? zip? mime type = application/zip
            // REF: https://msdn.microsoft.com/en-us/library/system.security.cryptography.x509certificates.x509certificate2.aspx

            using (var package = new TokenPackage("text/csv"))
                using (var writer = new StreamWriter(package, ASCII))
                {
                    foreach (var token in tokens)
                    {
                        var id         = Parse(token.Id);
                        var salt       = id.MintRequestId.ToByteArray();
                        var tokenCode  = tokenSecurity.DecryptFromBase64(token.Code, token.Hash, salt);
                        var @string    = Zero;
                        var fiveByFive = default(string);

                        try
                        {
                            @string    = SecureStringToGlobalAllocAnsi(tokenCode);
                            fiveByFive = PtrToStringAnsi(@string);
                        }
                        finally
                        {
                            if (@string != Zero)
                            {
                                ZeroFreeGlobalAllocAnsi(@string);
                            }
                        }

                        await writer.WriteAsync(token.Id);

                        await writer.WriteAsync(",");

                        await writer.WriteLineAsync(fiveByFive);
                    }

                    await writer.FlushAsync();

                    package.Position = 0L;
                    return(package);
                }
        }
Beispiel #3
0
        Token DecryptFiveByFive(Token token)
        {
            var salt = TokenIdentifier.Parse(token.Id).MintRequestId.ToByteArray();

            using (var tokenCode = tokenSecurity.DecryptFromBase64(token.Code, token.Hash, salt))
            {
                var fiveByFive = SecureStringToGlobalAllocAnsi(tokenCode);

                try
                {
                    token.Code = PtrToStringAnsi(fiveByFive);
                }
                finally
                {
                    if (fiveByFive != IntPtr.Zero)
                    {
                        ZeroFreeGlobalAllocAnsi(fiveByFive);
                    }
                }
            }

            return(token);
        }