using System;
using System.Runtime.InteropServices;
using System.Security;

namespace eSuite.WorkBench.Helpers
{
    public class SecureStringDecoder : IDisposable
    {
        private readonly SecureString _secureString;

        IntPtr intPtr = IntPtr.Zero;

        public SecureStringDecoder(SecureString secureString)
        {
            _secureString = secureString;         
            intPtr = Marshal.SecureStringToGlobalAllocUnicode(_secureString);
        }

        public string DecodeString()
        {
            return Marshal.PtrToStringUni(intPtr);
        }

        public void Dispose()
        {
            Marshal.ZeroFreeGlobalAllocUnicode(intPtr);
        }
    }
}
