Coding Challenge #2

Status
This thread has been locked.

Sonnet

History
Banned
Feedback score
0
Posts
106
Reactions
69
Resources
0
You are a systems admin and have discovered a flaw within your system, the users aren't alphabetically listed within your DB, you hate that shit, so you pull the list and decide to write a program that saves the users and passwords into a file and lists them alphabetically, along with encrypting each password with SHA.

Code:
User:Password
arb:1234
trp:5627
yah:6728
yte:6263
thf:9028
muh:9732
twq:8926
dhh:8765
cbv:0916
cbh:7285
dfg:8294
kah:9274
kak:9273
apk:5284
gah:9364
kaj:7484

Your objective:
Save the above list into a file, open the file within a program and list each user alphabetically, encrypt each password using SHA (your choice which one just specify) then overwrite the file and output the information.

For bonus points double encrypt the passwords, so it's harder to crack them.

Whatever language you want to use, good luck.
 
Last edited:
Banned forever. Reason: Scamming (https://builtbybit.com/threads/sonnet-scam-report.106789/)
PebbleHost
High performance, consistent uptime and fast support. Minecraft hosting that just works.

Sonnet

History
Banned
Feedback score
0
Posts
106
Reactions
69
Resources
0
there
plz do this instead
Code:
User:Password
arb:1234
trp:5627
yah:6728
yte:6263
thf:9028
muh:9732
twq:8926
dhh:8765
cbv:0916
cbh:7285
dfg:8294
kah:9274
kak:9273
apk:5284
gah:9364
kaj:7484
 
Banned forever. Reason: Scamming (https://builtbybit.com/threads/sonnet-scam-report.106789/)

Nagi

PM Only - No Skype
Supreme
Feedback score
12
Posts
535
Reactions
679
Resources
0
I'm guessing that username don't repeat.
Otherwise, you can't do what I did below.

Code:
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.TreeMap;

public class Challenge {

    public static void main(String[] args) throws IOException, NoSuchAlgorithmException, UnsupportedEncodingException {

        File inputFile = new File("Input.txt");
        BufferedReader reader = new BufferedReader(new FileReader(inputFile));

        TreeMap<String, String> userIndex = new TreeMap<String, String>();

        String readLine;
        while ((readLine = reader.readLine()) != null) {
            String[] userSplit = readLine.split(":");
            userIndex.put(userSplit[0], userSplit[1]);
        }

        reader.close();

        PrintWriter writer = new PrintWriter(inputFile);

        for (String username : userIndex.keySet())
            writer.println(username + ":" + toSHA256(userIndex.get(username)));

        writer.close();

    }

    public static String toSHA256(String arg0) throws NoSuchAlgorithmException, UnsupportedEncodingException {
        MessageDigest md = MessageDigest.getInstance("SHA-256");
        md.update(arg0.getBytes("UTF-8"));
        byte[] digest = md.digest();
        return String.format("%064x", new BigInteger(1, digest));
    }
}

Test Case 1:
Input File: https://www.kthisiscvpv.com/8yIug145614857920Fs0.txt
Output File: https://www.kthisiscvpv.com/8xyxP1456148604xjTY5.txt

Test Case 2:
Input File: https://www.kthisiscvpv.com/kZuYX1456148662UQws1.txt
Output File: https://www.kthisiscvpv.com/vRtmy14561487231Eb1a.txt
 
Last edited:

Kuh

Feedback score
0
Posts
19
Reactions
11
Resources
0
Lua. I used this implementation of sha256 (had to remove the "local" in line 186) and used Computercraft to test this :)
Code:
os.loadAPI("sha")

local datafile = "data.txt"
firstline = "User:Password"
local lines = {}

local file = io.open(datafile, "r")
for line in file:lines() do
  if line ~= firstline then
    table.insert(lines, line)
  end
end
file:close()

table.sort(lines)

file = io.open(datafile, "w")
file:write(firstline)
for _, line in ipairs(lines) do
  for name, pass in string.gmatch(line,"([^:]+):([^:]+)") do
    local newline = "\n"..name..":"..sha.sha256(pass)
    file:write(newline)
    write(newline)
  end
end
file:close()
 
Status
This thread has been locked.
Top