How can I hide password in file using python - list

File1 which has content
Username = xyz
password = 1234
userId = abc
sshkey = pqr1
username = ghq
userPassword = 456
File2 which I want to create and tranfer all the data from File1 to File2 but need to change all the password = ****, sskey = **** etc.
Coding I did:
list1 = ["password", "sshkey"]
new_list = []
content_str = ""
with open("file.txt") as content:
contents = content.readlines()
with open("file2.txt", "w") as f2:
for l in contents:
for data in list1:
if data in l.lower():
pas = l.split("=")
pas = pas[1] + "=" +"***"
new_list.append(pas)
else:
new_list.append(l)
content_str = content_str.join(new_list)
f2.write(content_str)
Now I want once I create new file so all the password or sshkey etc has to be ****
I tried a lot of different ways but didn't succeed.

f2.write("****") does the trick...
Nothing fancy about it. Of course you lose all the original data, you can't have it both ways.
That's the eternal problem storing secret information, at some point you must have a cleartext password of some sort to decrypt everything else.

Related

Django: Problem with ContentFile: String content is not downloaded completely

I'm quite new to using Django. As first project I wrote a little tool to create M3U8-Playlists.
My problem is, that long playlists are not transferred completely.
This is the stripped-down code:
def create(request):
# just a dummy filelist
playlist = ["#EXTM3U"] + 5 * ["/home/pi/music/" + 5 * "äöü0123456789á/" + "xyz.mp3"]
file_to_send = ContentFile("")
for item in playlist:
file_to_send.write("{}\n".format(item.replace("/home/pi/Music", r"\\raspberry\music").replace("/", "\\")))
response = HttpResponse(file_to_send, "audio/x-mpegurl")
response["Content-Length"] = file_to_send.size
response["Content-Disposition"] = f"attachment; filename=\"playlist.m3u8\""
# print some debug info
print("lines:", len(playlist), "chars (no linebreaks)", sum([len(entry) for entry in playlist]),
"filesize:", file_to_send.size)
return response
The problem seems to lie in the non-ascii chars in playlist entries (äöüá). When there are no such characters, the file is transferred intact. I assume that these are characters that use two bytes in UTF-8, but writing strings to the ContentFile like I do is probably not correct.
Found the answer, while working on the problem description.
This works:
def create(request):
# just a dummy filelist
playlist = ["#EXTM3U"] + 5 * ["/home/pi/music/" + 5 * "äöü0123456789á/" + "xyz.mp3"]
joined_playlist = "\n".join([item.replace("/home/pi/Music", r"\\raspberry\music").replace("/", "\\") for item in playlist])
file_to_send = ContentFile(joined_playlist.encode("UTF-8"))
response = HttpResponse(file_to_send, "audio/x-mpegurl")
response["Content-Length"] = file_to_send.size
response["Content-Disposition"] = f"attachment; filename=\"playlist.m3u8\""
# print some debug info
print("lines:", len(playlist), "chars (no linebreaks)", sum([len(entry) for entry in playlist]),
"filesize:", file_to_send.size)
return response
The important difference is, that I don't write Strings to the ContentFile any longer, but a byte array, which I got through encoding the String in UTF-8.
HTH

How to put regex info into hash

I need to parse an Apache log file and output IP, URL, and URL status code into hashes but don't know how to put the elements into a hash.
My code uses regular expressions to get the info I need from each line of the log file:
line_array = File.readlines("access_log")
line_array.each { |line| }
#regexp
md = (/^([:\d\.]+) .*\[.*\].*\"[A-Z]+ *(.+) HTTP\S*\s(\d+)/).match(line)
ip = md[1]
url = md[2]
status = md[3]
Is my current code even on the right track to be able to do this?
I need the hashes to display the item and then the frequency of said item. So if we have the hash for ip addresses it will display the ip addresses in the log file followed the the frequency of that specific ip.
Assuming your md correctly returns an array of strings which is what you appear to be expecting, then try:
line_array = File.readlines("access_log")
line_array.each { |line| }
#regexp
md = (/^([:\d\.]+) .*\[.*\].*\"[A-Z]+ *(.+) HTTP\S*\s(\d+)/).match(line)
hash = Hash.new
hash[:ip] = md[1]
hash[:url] = md[2]
hash[:status] = md[3]
This will create a hash object with the three keys:
hash
=> { ip: 'whatever is in md[1]', url: 'whatever is in md[2]', status: 'whatever is in md[3]' }
Also just to highlight, by accessing md[1] you're accessing the second element of the array, if you want the first you need md[0]

RegEx for domain name extraction from Adblock rule

I am trying to extract all domains from a text file and save it into another text file, but it displays all the domain names and other things, and it also returns:
ads.css
abc.js
Kashi.png
My input strings are:
token$script,domain=liveresult.ru
euroiphone.eu##.div-download-h
||ausujet.com/skins/common/ads.js
##||cyberdean.fr/js/advertisement.js
biggestplayer.me##.adblock + *
hearthhead.com,wowhead.com##.block-bg
wowhead.com##.block-bgimg
euroiphone.eu##.div-download-h
euroiphone.eu##.div-download-v
findretros.com##.fuck-adblock
##||ausujet.com/skins/common/ads.js
##||cyberdean.fr/js/advertisement.js
##||dbz-fantasy.com/ads.css
##||dev-dyod.fr/styles/ads.css
forums.ru###mdl_adb
ostroh.info###modal.modal-bg
7days2die.info###nafikblock
all-episodes.net###odin
There is lot of rule from which I have to extract domain
My result should be:
liveresult.ru
cyberdean.fr
euroiphone.eu
ausujet.com
biggestplayer.me
hearthhead.com
wowhead.com
euroiphone.eu
ausujet.com
cyberdean.fr
dbz-fantasy.com
dev-dyod.frforums.ru
7days2die.infoy
I have tried:
import re
Domains = ['ru', 'fr' ,'eu', 'com']
with open('easylist.txt', 'r') as f:
a=f.read()
result=re.findall(r'[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+',a)
unique_result = list(set(result))
for r in result:
domain_name = r.split('.')[1]
If domain_name in domains:
file_out.write(r+/n)
But for this I have to make a list of domain which is labours process , I want to make some pattern which automatically extract domain ignoring the things like ads.js , ads.css , advertisement.js etc , so kindly tell me where I m doing wrong .
If you want to print everything in a new line you should do file_out.write(r+'\n') to write every string on a new line, and you can remove duplicates by using set
import re
domains = ['ru', 'fr' ,'eu', 'com']
with open('easylist.txt', 'r') as f:
a=f.read()
result=re.findall(r'[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+',a)
unique_result = list(set(result))
for r in result:
#Extract domain name out of url
domain_name = r.split('.')[1]
#Check if domain name is in list of domains, only then add it
if domain_name in domains:
file_out.write(r)

Not able to pre populate the custom field using c# code in HelloSign

I was trying to pre-populate the custom field value from C# code but I was not able to. I have used following snippet of code to do so
var client = new Client("MY API KEY");
var request = new TemplateSignatureRequest();
request.AddTemplate("docID");
request.Title = "Test Titel";
request.Subject = "Test Subject";
request.Message = "Test Message.";
request.AddSigner("Me", "xxxxx#example.com", "Test");
request.AddSigner("Client", "xxxxx#example.com", "Test");
var firstName = request.GetCustomField("FirstName");
firstName.Value = "John";
request.TestMode = true;
var response = client.SendSignatureRequest(request);
I have already assigned the customer field to me and verified the API ID key for the field also.But still not able to get the result. Is there anything else we need to add to so that value John will be shown.
Try
request.AddCustomField("fName", "Alex");
Also, please feel free to reach out to apisupport#hellosign.com for more assistance if you would like.

Dashcode scroll area refresh with multiple writes

I'm creating a Dashcode project that allows me to mount remote drives. I have a Scroll Area named "statusArea" defined to contain status text of the mounting progress, kind of a command output area.
The first time through testing I'll force the "ERROR: Must enter " text to be written. Then I'll go to the back to provide a username and password and try again, but the box is never cleared for new text.
// get configuration fields from back panel
var usernameField = document.getElementById("username");
var username = usernameField.value;
var passwordField = document.getElementById("password");
var password = passwordField.value;
// clear status
var statusArea = document.getElementById("statusArea");
var status = document.getElementById("content");
status.innerText = "";
statusArea.object.refresh();
if ( !username || !password) {
status.innerText = "ERROR: Must enter username and password on back panel!";
alert("Must enter username and password on back panel!");
}
if (username && password) {
status.innerText = "Connecting as "+username;
Am I going about this the wrong way?
Thanks,
Bill