Skip to content

Instantly share code, notes, and snippets.

@jennybc
Created January 20, 2024 16:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jennybc/6cc6b212ea901a9059156949812d24ad to your computer and use it in GitHub Desktop.
Save jennybc/6cc6b212ea901a9059156949812d24ad to your computer and use it in GitHub Desktop.
Noodling with the GitHub PAT checksum
pat <- gitcreds::gitcreds_get()
cksum <- digest::digest(
substr(pat$password, start = 5, stop = nchar(pat$password) - 6),
algo = "crc32", serialize = FALSE
)
# https://stackoverflow.com/q/5478508
toBase <- function(num, base=62) {
# bv <- c(seq(0,9),letters,LETTERS)
# GitHub seems to do UPPERCASE then lowercase
bv <- c(seq(0,9), LETTERS, letters)
r <- num %% base
res <- bv[r+1]
q <- floor(num/base)
while (q > 0L) {
r <- q %% base
q <- floor(q/base)
res <- paste(bv[r+1], res, sep='')
}
res
}
to10 <- function(num, base=62) {
bv <- c(seq(0,9),letters,LETTERS)
vb <- list()
for (i in 1:length(bv)) vb[[bv[i]]] <- i
num <- strsplit(num,'')[[1]]
res <- vb[[num[1]]]-1
if (length(num) > 1)
for (i in 2:length(num)) res <- base * res + (vb[[num[i]]]-1)
res
}
(expected <- toBase(to10(cksum, base = 16)))
(actual <- substr(pat$password, start = 35, stop = 40))
testthat::expect_equal(expected, actual)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment